To build (i.e., compile and link) your libpq programs you need to do all of the following things:
#include <libpq-fe.h>If you failed to do that then you will normally get error messages from your compiler similar to
foo.c: In function `main': foo.c:34: `PGconn' undeclared (first use in this function) foo.c:35: `PGresult' undeclared (first use in this function) foo.c:54: `CONNECTION_BAD' undeclared (first use in this function) foo.c:68: `PGRES_COMMAND_OK' undeclared (first use in this function) foo.c:95: `PGRES_TUPLES_OK' undeclared (first use in this function)
cc -c -I/usr/local/pgsql/include testprog.cIf you are using makefiles then add the option to the CPPFLAGS variable:
CPPFLAGS += -I/usr/local/pgsql/includeIf there is any chance that your program might be compiled by other users then you should not hardcode the directory location like that. Instead, you can run the utility pg_config to find out where the header files are on the local system:
$ pg_config --includedir /usr/local/includeFailure to specify the correct option to the compiler will result in an error message such as
testlibpq.c:8:22: libpq-fe.h: No such file or directory
cc -o testprog testprog1.o testprog2.o -L/usr/local/pgsql/lib -lpqYou can find out the library directory using pg_config as well:
$ pg_config --libdir /usr/local/pgsql/libError messages that point to problems in this area could look like the following.
testlibpq.o: In function `main': testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin' testlibpq.o(.text+0x71): undefined reference to `PQstatus' testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage'This means you forgot -lpq.
/usr/bin/ld: cannot find -lpqThis means you forgot the -L option or did not specify the right directory.
If your codes references the header file libpq-int.h and you refuse to fix your code to not use it, starting in PostgreSQL 7.2, this file will be found in includedir/postgresql/internal/libpq-int.h, so you need to add the appropriate -I option to your compiler command line.