]> begriffs open source - ai-pg/blob - full-docs/txt/libpq-build.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / libpq-build.txt
1
2 32.22. Building libpq Programs #
3
4    To build (i.e., compile and link) a program using libpq you need to do
5    all of the following things:
6      * Include the libpq-fe.h header file:
7 #include <libpq-fe.h>
8
9        If you failed to do that then you will normally get error messages
10        from your compiler similar to:
11 foo.c: In function `main':
12 foo.c:34: `PGconn' undeclared (first use in this function)
13 foo.c:35: `PGresult' undeclared (first use in this function)
14 foo.c:54: `CONNECTION_BAD' undeclared (first use in this function)
15 foo.c:68: `PGRES_COMMAND_OK' undeclared (first use in this function)
16 foo.c:95: `PGRES_TUPLES_OK' undeclared (first use in this function)
17
18      * Point your compiler to the directory where the PostgreSQL header
19        files were installed, by supplying the -Idirectory option to your
20        compiler. (In some cases the compiler will look into the directory
21        in question by default, so you can omit this option.) For instance,
22        your compile command line could look like:
23 cc -c -I/usr/local/pgsql/include testprog.c
24
25        If you are using makefiles then add the option to the CPPFLAGS
26        variable:
27 CPPFLAGS += -I/usr/local/pgsql/include
28
29        If there is any chance that your program might be compiled by other
30        users then you should not hardcode the directory location like
31        that. Instead, you can run the utility pg_config to find out where
32        the header files are on the local system:
33 $ pg_config --includedir
34 /usr/local/include
35
36        If you have pkg-config installed, you can run instead:
37 $ pkg-config --cflags libpq
38 -I/usr/local/include
39
40        Note that this will already include the -I in front of the path.
41        Failure to specify the correct option to the compiler will result
42        in an error message such as:
43 testlibpq.c:8:22: libpq-fe.h: No such file or directory
44
45      * When linking the final program, specify the option -lpq so that the
46        libpq library gets pulled in, as well as the option -Ldirectory to
47        point the compiler to the directory where the libpq library
48        resides. (Again, the compiler will search some directories by
49        default.) For maximum portability, put the -L option before the
50        -lpq option. For example:
51 cc -o testprog testprog1.o testprog2.o -L/usr/local/pgsql/lib -lpq
52
53        You can find out the library directory using pg_config as well:
54 $ pg_config --libdir
55 /usr/local/pgsql/lib
56
57        Or again use pkg-config:
58 $ pkg-config --libs libpq
59 -L/usr/local/pgsql/lib -lpq
60
61        Note again that this prints the full options, not only the path.
62        Error messages that point to problems in this area could look like
63        the following:
64 testlibpq.o: In function `main':
65 testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin'
66 testlibpq.o(.text+0x71): undefined reference to `PQstatus'
67 testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage'
68
69        This means you forgot -lpq.
70 /usr/bin/ld: cannot find -lpq
71
72        This means you forgot the -L option or did not specify the right
73        directory.