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.10. Processing Embedded SQL 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="ecpg-preproc.html" title="34.9. Preprocessor Directives" /><link rel="next" href="ecpg-library.html" title="34.11. Library Functions" /></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.10. Processing Embedded SQL Programs</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-preproc.html" title="34.9. Preprocessor Directives">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg.html" title="Chapter 34. ECPG — Embedded SQL in C">Up</a></td><th width="60%" align="center">Chapter 34. <span class="application">ECPG</span> — Embedded <acronym class="acronym">SQL</acronym> in C</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-library.html" title="34.11. Library Functions">Next</a></td></tr></table><hr /></div><div class="sect1" id="ECPG-PROCESS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">34.10. Processing Embedded SQL Programs <a href="#ECPG-PROCESS" class="id_link">#</a></h2></div></div></div><p>
3 Now that you have an idea how to form embedded SQL C programs, you
4 probably want to know how to compile them. Before compiling you
5 run the file through the embedded <acronym class="acronym">SQL</acronym>
6 <acronym class="acronym">C</acronym> preprocessor, which converts the
7 <acronym class="acronym">SQL</acronym> statements you used to special function
8 calls. After compiling, you must link with a special library that
9 contains the needed functions. These functions fetch information
10 from the arguments, perform the <acronym class="acronym">SQL</acronym> command using
11 the <span class="application">libpq</span> interface, and put the result
12 in the arguments specified for output.
14 The preprocessor program is called <code class="filename">ecpg</code> and is
15 included in a normal <span class="productname">PostgreSQL</span> installation.
16 Embedded SQL programs are typically named with an extension
17 <code class="filename">.pgc</code>. If you have a program file called
18 <code class="filename">prog1.pgc</code>, you can preprocess it by simply
20 </p><pre class="programlisting">
23 This will create a file called <code class="filename">prog1.c</code>. If
24 your input files do not follow the suggested naming pattern, you
25 can specify the output file explicitly using the
26 <code class="option">-o</code> option.
28 The preprocessed file can be compiled normally, for example:
29 </p><pre class="programlisting">
32 The generated C source files include header files from the
33 <span class="productname">PostgreSQL</span> installation, so if you installed
34 <span class="productname">PostgreSQL</span> in a location that is not searched by
35 default, you have to add an option such as
36 <code class="literal">-I/usr/local/pgsql/include</code> to the compilation
39 To link an embedded SQL program, you need to include the
40 <code class="filename">libecpg</code> library, like so:
41 </p><pre class="programlisting">
42 cc -o myprog prog1.o prog2.o ... -lecpg
44 Again, you might have to add an option like
45 <code class="literal">-L/usr/local/pgsql/lib</code> to that command line.
48 use <code class="command">pg_config</code><a id="id-1.7.5.16.6.2" class="indexterm"></a>
49 or <code class="command">pkg-config</code><a id="id-1.7.5.16.6.4" class="indexterm"></a> with package name <code class="literal">libecpg</code> to
50 get the paths for your installation.
52 If you manage the build process of a larger project using
53 <span class="application">make</span>, it might be convenient to include
54 the following implicit rule to your makefiles:
55 </p><pre class="programlisting">
62 The complete syntax of the <code class="command">ecpg</code> command is
63 detailed in <a class="xref" href="app-ecpg.html" title="ecpg"><span class="refentrytitle"><span class="application">ecpg</span></span></a>.
65 The <span class="application">ecpg</span> library is thread-safe by
66 default. However, you might need to use some threading
67 command-line options to compile your client code.
68 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-preproc.html" title="34.9. Preprocessor Directives">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg.html" title="Chapter 34. ECPG — Embedded SQL in C">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-library.html" title="34.11. Library Functions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">34.9. Preprocessor Directives </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 34.11. Library Functions</td></tr></table></div></body></html>