|
1 | 1 | <!--
|
2 |
| -$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.66 2001/08/10 22:50:09 tgl Exp $ |
| 2 | +$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.67 2001/08/28 14:20:25 petere Exp $ |
3 | 3 | -->
|
4 | 4 |
|
5 | 5 | <chapter id="libpq">
|
@@ -1994,6 +1994,129 @@ call <function>fe_setauthsvc</function> at all.
|
1994 | 1994 | </sect1>
|
1995 | 1995 |
|
1996 | 1996 |
|
| 1997 | + <sect1 id="libpq-build"> |
| 1998 | + <title>Building Libpq Programs</title> |
| 1999 | + |
| 2000 | + <para> |
| 2001 | + To build (i.e., compile and link) your libpq programs you need to |
| 2002 | + do the following things: |
| 2003 | + |
| 2004 | + <itemizedlist> |
| 2005 | + <listitem> |
| 2006 | + <para> |
| 2007 | + Include the <filename>libpq-fe.h</filename> header file: |
| 2008 | +<programlisting> |
| 2009 | +#include <libpq-fe> |
| 2010 | +</programlisting> |
| 2011 | + If you failed to do that then you will normally get error |
| 2012 | + messages from your compiler, such as |
| 2013 | +<screen> |
| 2014 | +foo.c: In function `main': |
| 2015 | +foo.c:34: `PGconn' undeclared (first use in this function) |
| 2016 | +foo.c:35: `PGresult' undeclared (first use in this function) |
| 2017 | +foo.c:54: `CONNECTION_BAD' undeclared (first use in this function) |
| 2018 | +foo.c:68: `PGRES_COMMAND_OK' undeclared (first use in this function) |
| 2019 | +foo.c:95: `PGRES_TUPLES_OK' undeclared (first use in this function) |
| 2020 | +</screen> |
| 2021 | + </para> |
| 2022 | + </listitem> |
| 2023 | + |
| 2024 | + <listitem> |
| 2025 | + <para> |
| 2026 | + Point your compiler to the directory where the PostgreSQL header |
| 2027 | + files were installed, by supplying the |
| 2028 | + <literal>-I<replaceable>directory</replaceable></literal> option |
| 2029 | + to your compiler. (In some cases the compiler will look into |
| 2030 | + the directory in question by default, so you can omit this |
| 2031 | + option.) For instance, your compile command line could look |
| 2032 | + like: |
| 2033 | +<programlisting> |
| 2034 | +cc -c -I/usr/local/pgsql/include testprog.c |
| 2035 | +</programlisting> |
| 2036 | + If you are using makefiles then add the option to the |
| 2037 | + <varname>CPPFLAGS</varname> variable: |
| 2038 | +<programlisting> |
| 2039 | +CPPFLAGS += -I/usr/local/pgsql/include |
| 2040 | +</programlisting> |
| 2041 | + </para> |
| 2042 | + |
| 2043 | + <para> |
| 2044 | + If there is any chance that your program might be compiled by |
| 2045 | + other users then you should not hardcode the directory location |
| 2046 | + like that. Instead, you can run the utility |
| 2047 | + <command>pg_config</command> to find out where the header files |
| 2048 | + are on the local system: |
| 2049 | +<screen> |
| 2050 | +<prompt>$</prompt> pg_config --includedir |
| 2051 | +<computeroutput>/usr/local/include</computeroutput> |
| 2052 | +</screen> |
| 2053 | + </para> |
| 2054 | + |
| 2055 | + <para> |
| 2056 | + Failure to specify the correct option to the compiler will |
| 2057 | + result in an error message such as |
| 2058 | +<screen> |
| 2059 | +testlibpq.c:8:22: libpq-fe.h: No such file or directory |
| 2060 | +</screen> |
| 2061 | + </para> |
| 2062 | + </listitem> |
| 2063 | + |
| 2064 | + <listitem> |
| 2065 | + <para> |
| 2066 | + When linking the final program, specify the option |
| 2067 | + <literal>-lpq</literal> so that the libpq library gets pulled |
| 2068 | + in, as well as the option |
| 2069 | + <literal>-L<replaceable>directory</replaceable></literal> to |
| 2070 | + point it to the directory where libpq resides. (Again, the |
| 2071 | + compiler will search some directories by default.) For maximum |
| 2072 | + portability, put the <option>-L</option> option before the |
| 2073 | + <option>-lpq</option> option. For example: |
| 2074 | +<programlisting> |
| 2075 | +cc -o testprog testprog1.o testprog2.o -L/usr/local/pgsql/lib -lpq |
| 2076 | +</programlisting> |
| 2077 | + </para> |
| 2078 | + |
| 2079 | + <para> |
| 2080 | + You can find out the library directory using |
| 2081 | + <command>pg_config</command> as well: |
| 2082 | +<screen> |
| 2083 | +<prompt>$</prompt> pg_config --libdir |
| 2084 | +<computeroutput>/usr/local/pgsql/lib</computeroutput> |
| 2085 | +</screen> |
| 2086 | + </para> |
| 2087 | + |
| 2088 | + <para> |
| 2089 | + Error messages that point to problems in this area could look |
| 2090 | + like the following. |
| 2091 | +<screen> |
| 2092 | +testlibpq.o: In function `main': |
| 2093 | +testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin' |
| 2094 | +testlibpq.o(.text+0x71): undefined reference to `PQstatus' |
| 2095 | +testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage' |
| 2096 | +</screen> |
| 2097 | + This means you forgot <option>-lpq</option>. |
| 2098 | +<screen> |
| 2099 | +/usr/bin/ld: cannot find -lpq |
| 2100 | +</screen> |
| 2101 | + This means you forgot the <option>-L</option> or did not specify |
| 2102 | + the right path. |
| 2103 | + </para> |
| 2104 | + </listitem> |
| 2105 | + </itemizedlist> |
| 2106 | + </para> |
| 2107 | + |
| 2108 | + <para> |
| 2109 | + If your codes references the header file |
| 2110 | + <filename>libpq-int.h</filename> and you refuse to fix your code to |
| 2111 | + not use it, starting in PostgreSQL 7.2, this file will be found in |
| 2112 | + <filename><replaceable>includedir</replaceable>/postgresql/internal/libpq-int.h</filename>, |
| 2113 | + so you need to add the appropriate <option>-I</option> option to |
| 2114 | + your compiler command line. |
| 2115 | + </para> |
| 2116 | + |
| 2117 | + </sect1> |
| 2118 | + |
| 2119 | + |
1997 | 2120 | <sect1 id="libpq-example">
|
1998 | 2121 | <title>Example Programs</title>
|
1999 | 2122 |
|
|
0 commit comments