Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 9b6cc37ce608401d44f6535a0c7cb777 > files > 468

postgresql11-docs-11.5-1.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>34.20. Building libpq Programs</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="libpq-threading.html" title="34.19. Behavior in Threaded Programs" /><link rel="next" href="libpq-example.html" title="34.21. Example Programs" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">34.20. Building <span xmlns="http://www.w3.org/1999/xhtml" class="application">libpq</span> Programs</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-threading.html" title="34.19. Behavior in Threaded Programs">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 34. libpq - C Library">Up</a></td><th width="60%" align="center">Chapter 34. <span xmlns="http://www.w3.org/1999/xhtml" class="application">libpq</span> - C Library</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 11.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="libpq-example.html" title="34.21. Example Programs">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LIBPQ-BUILD"><div class="titlepage"><div><div><h2 class="title" style="clear: both">34.20. Building <span class="application">libpq</span> Programs</h2></div></div></div><a id="id-1.7.3.27.2" class="indexterm"></a><p>
   To build (i.e., compile and link) a program using
   <span class="application">libpq</span> you need to do all of the following
   things:

   </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
      Include the <code class="filename">libpq-fe.h</code> header file:
</p><pre class="programlisting">
#include &lt;libpq-fe.h&gt;
</pre><p>
      If you failed to do that then you will normally get error messages
      from your compiler similar to:
</p><pre class="screen">
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)
</pre><p>
     </p></li><li class="listitem"><p>
      Point your compiler to the directory where the <span class="productname">PostgreSQL</span> header
      files were installed, by supplying the
      <code class="literal">-I<em class="replaceable"><code>directory</code></em></code> option
      to your compiler.  (In some cases the compiler will look into
      the directory in question by default, so you can omit this
      option.)  For instance, your compile command line could look
      like:
</p><pre class="programlisting">
cc -c -I/usr/local/pgsql/include testprog.c
</pre><p>
      If you are using makefiles then add the option to the
      <code class="varname">CPPFLAGS</code> variable:
</p><pre class="programlisting">
CPPFLAGS += -I/usr/local/pgsql/include
</pre><p>
     </p><p>
      If 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
      <code class="command">pg_config</code><a id="id-1.7.3.27.3.2.2.2.2" class="indexterm"></a> to find out where the header
      files are on the local system:
</p><pre class="screen">
<code class="prompt">$</code> pg_config --includedir
<code class="computeroutput">/usr/local/include</code>
</pre><p>
     </p><p>
      If you
      have <code class="command">pkg-config</code><a id="id-1.7.3.27.3.2.2.3.2" class="indexterm"></a> installed, you can run instead:
</p><pre class="screen">
<code class="prompt">$</code> pkg-config --cflags libpq
<code class="computeroutput">-I/usr/local/include</code>
</pre><p>
      Note that this will already include the <code class="option">-I</code> in front of
      the path.
     </p><p>
      Failure to specify the correct option to the compiler will
      result in an error message such as:
</p><pre class="screen">
testlibpq.c:8:22: libpq-fe.h: No such file or directory
</pre><p>
     </p></li><li class="listitem"><p>
      When linking the final program, specify the option
      <code class="literal">-lpq</code> so that the <span class="application">libpq</span>
      library gets pulled in, as well as the option
      <code class="literal">-L<em class="replaceable"><code>directory</code></em></code> to point
      the compiler to the directory where the
      <span class="application">libpq</span> library resides.  (Again, the
      compiler will search some directories by default.)  For maximum
      portability, put the <code class="option">-L</code> option before the
      <code class="option">-lpq</code> option.  For example:
</p><pre class="programlisting">
cc -o testprog testprog1.o testprog2.o -L/usr/local/pgsql/lib -lpq
</pre><p>
     </p><p>
      You can find out the library directory using
      <code class="command">pg_config</code> as well:
</p><pre class="screen">
<code class="prompt">$</code> pg_config --libdir
<code class="computeroutput">/usr/local/pgsql/lib</code>
</pre><p>
     </p><p>
      Or again use <code class="command">pkg-config</code>:
</p><pre class="screen">
<code class="prompt">$</code> pkg-config --libs libpq
<code class="computeroutput">-L/usr/local/pgsql/lib -lpq</code>
</pre><p>
      Note again that this prints the full options, not only the path.
     </p><p>
      Error messages that point to problems in this area could look like
      the following:
</p><pre class="screen">
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'
</pre><p>
      This means you forgot <code class="option">-lpq</code>.
</p><pre class="screen">
/usr/bin/ld: cannot find -lpq
</pre><p>
      This means you forgot the <code class="option">-L</code> option or did not
      specify the right directory.
     </p></li></ul></div><p>
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-threading.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-example.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">34.19. Behavior in Threaded Programs </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 34.21. Example Programs</td></tr></table></div></body></html>