gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
7.1KB

  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>33.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 V1.79.1" /><link rel="prev" href="libpq-threading.html" title="33.19. Behavior in Threaded Programs" /><link rel="next" href="libpq-example.html" title="33.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">33.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="33.19. Behavior in Threaded Programs">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 33. libpq - C Library">Up</a></td><th width="60%" align="center">Chapter 33. <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 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="libpq-example.html" title="33.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">33.20. Building <span class="application">libpq</span> Programs</h2></div></div></div><a id="id-1.7.3.27.2" class="indexterm"></a><p>
  3. To build (i.e., compile and link) a program using
  4. <span class="application">libpq</span> you need to do all of the following
  5. things:
  6. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  7. Include the <code class="filename">libpq-fe.h</code> header file:
  8. </p><pre class="programlisting">
  9. #include &lt;libpq-fe.h&gt;
  10. </pre><p>
  11. If you failed to do that then you will normally get error messages
  12. from your compiler similar to:
  13. </p><pre class="screen">
  14. foo.c: In function `main':
  15. foo.c:34: `PGconn' undeclared (first use in this function)
  16. foo.c:35: `PGresult' undeclared (first use in this function)
  17. foo.c:54: `CONNECTION_BAD' undeclared (first use in this function)
  18. foo.c:68: `PGRES_COMMAND_OK' undeclared (first use in this function)
  19. foo.c:95: `PGRES_TUPLES_OK' undeclared (first use in this function)
  20. </pre><p>
  21. </p></li><li class="listitem"><p>
  22. Point your compiler to the directory where the <span class="productname">PostgreSQL</span> header
  23. files were installed, by supplying the
  24. <code class="literal">-I<em class="replaceable"><code>directory</code></em></code> option
  25. to your compiler. (In some cases the compiler will look into
  26. the directory in question by default, so you can omit this
  27. option.) For instance, your compile command line could look
  28. like:
  29. </p><pre class="programlisting">
  30. cc -c -I/usr/local/pgsql/include testprog.c
  31. </pre><p>
  32. If you are using makefiles then add the option to the
  33. <code class="varname">CPPFLAGS</code> variable:
  34. </p><pre class="programlisting">
  35. CPPFLAGS += -I/usr/local/pgsql/include
  36. </pre><p>
  37. </p><p>
  38. If there is any chance that your program might be compiled by
  39. other users then you should not hardcode the directory location
  40. like that. Instead, you can run the utility
  41. <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
  42. files are on the local system:
  43. </p><pre class="screen">
  44. <code class="prompt">$</code> pg_config --includedir
  45. <code class="computeroutput">/usr/local/include</code>
  46. </pre><p>
  47. </p><p>
  48. If you
  49. 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:
  50. </p><pre class="screen">
  51. <code class="prompt">$</code> pkg-config --cflags libpq
  52. <code class="computeroutput">-I/usr/local/include</code>
  53. </pre><p>
  54. Note that this will already include the <code class="option">-I</code> in front of
  55. the path.
  56. </p><p>
  57. Failure to specify the correct option to the compiler will
  58. result in an error message such as:
  59. </p><pre class="screen">
  60. testlibpq.c:8:22: libpq-fe.h: No such file or directory
  61. </pre><p>
  62. </p></li><li class="listitem"><p>
  63. When linking the final program, specify the option
  64. <code class="literal">-lpq</code> so that the <span class="application">libpq</span>
  65. library gets pulled in, as well as the option
  66. <code class="literal">-L<em class="replaceable"><code>directory</code></em></code> to point
  67. the compiler to the directory where the
  68. <span class="application">libpq</span> library resides. (Again, the
  69. compiler will search some directories by default.) For maximum
  70. portability, put the <code class="option">-L</code> option before the
  71. <code class="option">-lpq</code> option. For example:
  72. </p><pre class="programlisting">
  73. cc -o testprog testprog1.o testprog2.o -L/usr/local/pgsql/lib -lpq
  74. </pre><p>
  75. </p><p>
  76. You can find out the library directory using
  77. <code class="command">pg_config</code> as well:
  78. </p><pre class="screen">
  79. <code class="prompt">$</code> pg_config --libdir
  80. <code class="computeroutput">/usr/local/pgsql/lib</code>
  81. </pre><p>
  82. </p><p>
  83. Or again use <code class="command">pkg-config</code>:
  84. </p><pre class="screen">
  85. <code class="prompt">$</code> pkg-config --libs libpq
  86. <code class="computeroutput">-L/usr/local/pgsql/lib -lpq</code>
  87. </pre><p>
  88. Note again that this prints the full options, not only the path.
  89. </p><p>
  90. Error messages that point to problems in this area could look like
  91. the following:
  92. </p><pre class="screen">
  93. testlibpq.o: In function `main':
  94. testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin'
  95. testlibpq.o(.text+0x71): undefined reference to `PQstatus'
  96. testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage'
  97. </pre><p>
  98. This means you forgot <code class="option">-lpq</code>.
  99. </p><pre class="screen">
  100. /usr/bin/ld: cannot find -lpq
  101. </pre><p>
  102. This means you forgot the <code class="option">-L</code> option or did not
  103. specify the right directory.
  104. </p></li></ul></div><p>
  105. </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">33.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"> 33.21. Example Programs</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1