Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
libpq-build.html106 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!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.21. 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.20. Behavior in Threaded Programs" /><link rel="next" href="libpq-example.html" title="34.22. Example Programs" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">34.21. Building <span class="application">libpq</span> Programs</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-threading.html" title="34.20. 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 class="application">libpq</span> — C Library</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="libpq-example.html" title="34.22. Example Programs">Next</a></td></tr></table><hr /></div><div class="sect1" id="LIBPQ-BUILD"><div class="titlepage"><div><div><h2 class="title" style="clear: both">34.21. Building <span class="application">libpq</span> Programs <a href="#LIBPQ-BUILD" class="id_link">#</a></h2></div></div></div><a id="id-1.7.3.28.2" class="indexterm"></a><p>3   To build (i.e., compile and link) a program using4   <span class="application">libpq</span> you need to do all of the following5   things:6 7   </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>8      Include the <code class="filename">libpq-fe.h</code> header file:9</p><pre class="programlisting">10#include &lt;libpq-fe.h&gt;11</pre><p>12      If you failed to do that then you will normally get error messages13      from your compiler similar to:14</p><pre class="screen">15foo.c: In function `main':16foo.c:34: `PGconn' undeclared (first use in this function)17foo.c:35: `PGresult' undeclared (first use in this function)18foo.c:54: `CONNECTION_BAD' undeclared (first use in this function)19foo.c:68: `PGRES_COMMAND_OK' undeclared (first use in this function)20foo.c:95: `PGRES_TUPLES_OK' undeclared (first use in this function)21</pre><p>22     </p></li><li class="listitem"><p>23      Point your compiler to the directory where the <span class="productname">PostgreSQL</span> header24      files were installed, by supplying the25      <code class="literal">-I<em class="replaceable"><code>directory</code></em></code> option26      to your compiler.  (In some cases the compiler will look into27      the directory in question by default, so you can omit this28      option.)  For instance, your compile command line could look29      like:30</p><pre class="programlisting">31cc -c -I/usr/local/pgsql/include testprog.c32</pre><p>33      If you are using makefiles then add the option to the34      <code class="varname">CPPFLAGS</code> variable:35</p><pre class="programlisting">36CPPFLAGS += -I/usr/local/pgsql/include37</pre><p>38     </p><p>39      If there is any chance that your program might be compiled by40      other users then you should not hardcode the directory location41      like that.  Instead, you can run the utility42      <code class="command">pg_config</code><a id="id-1.7.3.28.3.2.2.2.2" class="indexterm"></a> to find out where the header43      files are on the local system:44</p><pre class="screen">45<code class="prompt">$</code> pg_config --includedir46<code class="computeroutput">/usr/local/include</code>47</pre><p>48     </p><p>49      If you50      have <code class="command">pkg-config</code><a id="id-1.7.3.28.3.2.2.3.2" class="indexterm"></a> installed, you can run instead:51</p><pre class="screen">52<code class="prompt">$</code> pkg-config --cflags libpq53<code class="computeroutput">-I/usr/local/include</code>54</pre><p>55      Note that this will already include the <code class="option">-I</code> in front of56      the path.57     </p><p>58      Failure to specify the correct option to the compiler will59      result in an error message such as:60</p><pre class="screen">61testlibpq.c:8:22: libpq-fe.h: No such file or directory62</pre><p>63     </p></li><li class="listitem"><p>64      When linking the final program, specify the option65      <code class="literal">-lpq</code> so that the <span class="application">libpq</span>66      library gets pulled in, as well as the option67      <code class="literal">-L<em class="replaceable"><code>directory</code></em></code> to point68      the compiler to the directory where the69      <span class="application">libpq</span> library resides.  (Again, the70      compiler will search some directories by default.)  For maximum71      portability, put the <code class="option">-L</code> option before the72      <code class="option">-lpq</code> option.  For example:73</p><pre class="programlisting">74cc -o testprog testprog1.o testprog2.o -L/usr/local/pgsql/lib -lpq75</pre><p>76     </p><p>77      You can find out the library directory using78      <code class="command">pg_config</code> as well:79</p><pre class="screen">80<code class="prompt">$</code> pg_config --libdir81<code class="computeroutput">/usr/local/pgsql/lib</code>82</pre><p>83     </p><p>84      Or again use <code class="command">pkg-config</code>:85</p><pre class="screen">86<code class="prompt">$</code> pkg-config --libs libpq87<code class="computeroutput">-L/usr/local/pgsql/lib -lpq</code>88</pre><p>89      Note again that this prints the full options, not only the path.90     </p><p>91      Error messages that point to problems in this area could look like92      the following:93</p><pre class="screen">94testlibpq.o: In function `main':95testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin'96testlibpq.o(.text+0x71): undefined reference to `PQstatus'97testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage'98</pre><p>99      This means you forgot <code class="option">-lpq</code>.100</p><pre class="screen">101/usr/bin/ld: cannot find -lpq102</pre><p>103      This means you forgot the <code class="option">-L</code> option or did not104      specify the right directory.105     </p></li></ul></div><p>106  </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" title="34.20. Behavior in Threaded Programs">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html" title="Chapter 34. libpq — C Library">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-example.html" title="34.22. Example Programs">Next</a></td></tr><tr><td width="40%" align="left" valign="top">34.20. Behavior in Threaded Programs </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 34.22. Example Programs</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai