1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>28.16.Building libpq Programs</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="libpq.html" title="Chapter28.libpq - C Library">
<link rel="prev" href="libpq-threading.html" title="28.15.Behavior in Threaded Programs">
<link rel="next" href="libpq-example.html" title="28.17.Example Programs">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="libpq-build"></a>28.16.Building <span class="application">libpq</span> Programs</h2></div></div></div>
<a name="id687085"></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 type="disc">
<li>
<p> Include the <code class="filename">libpq-fe.h</code> header file:
</p>
<pre class="programlisting">#include <libpq-fe.h></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>
<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 name="id687199"></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> 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>
<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> 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>
<p> <a name="id687358"></a>
If your codes references the header file
<code class="filename">libpq-int.h</code> and you refuse to fix your code to
not use it, starting in <span class="productname">PostgreSQL</span> 7.2, this file will be found in
<code class="filename"><em class="replaceable"><code>includedir</code></em>/postgresql/internal/libpq-int.h</code>,
so you need to add the appropriate <code class="option">-I</code> option to
your compiler command line.
</p>
</div></body>
</html>
|