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
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>30.11.Processing Embedded SQL 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="ecpg.html" title="Chapter30.ECPG - Embedded SQL in C">
<link rel="prev" href="ecpg-include.html" title="30.10.Including Files">
<link rel="next" href="ecpg-library.html" title="30.12.Library Functions">
<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="ecpg-process"></a>30.11.Processing Embedded SQL Programs</h2></div></div></div>
<p> Now that you have an idea how to form embedded SQL C programs, you
probably want to know how to compile them. Before compiling you
run the file through the embedded <acronym class="acronym">SQL</acronym>
<acronym class="acronym">C</acronym> preprocessor, which converts the
<acronym class="acronym">SQL</acronym> statements you used to special function
calls. After compiling, you must link with a special library that
contains the needed functions. These functions fetch information
from the arguments, perform the <acronym class="acronym">SQL</acronym> command using
the <span class="application">libpq</span> interface, and put the result
in the arguments specified for output.
</p>
<p> The preprocessor program is called <code class="filename">ecpg</code> and is
included in a normal <span class="productname">PostgreSQL</span> installation.
Embedded SQL programs are typically named with an extension
<code class="filename">.pgc</code>. If you have a program file called
<code class="filename">prog1.pgc</code>, you can preprocess it by simply
calling
</p>
<pre class="programlisting">ecpg prog1.pgc</pre>
<p>
This will create a file called <code class="filename">prog1.c</code>. If
your input files do not follow the suggested naming pattern, you
can specify the output file explicitly using the
<code class="option">-o</code> option.
</p>
<p> The preprocessed file can be compiled normally, for example:
</p>
<pre class="programlisting">cc -c prog1.c</pre>
<p>
The generated C source files include header files from the
<span class="productname">PostgreSQL</span> installation, so if you installed
<span class="productname">PostgreSQL</span> in a location that is not searched by
default, you have to add an option such as
<code class="literal">-I/usr/local/pgsql/include</code> to the compilation
command line.
</p>
<p> To link an embedded SQL program, you need to include the
<code class="filename">libecpg</code> library, like so:
</p>
<pre class="programlisting">cc -o myprog prog1.o prog2.o ... -lecpg</pre>
<p>
Again, you might have to add an option like
<code class="literal">-L/usr/local/pgsql/lib</code> to that command line.
</p>
<p> If you manage the build process of a larger project using
<span class="application">make</span>, it may be convenient to include
the following implicit rule to your makefiles:
</p>
<pre class="programlisting">ECPG = ecpg
%.c: %.pgc
$(ECPG) $<</pre>
<p>
</p>
<p> The complete syntax of the <code class="command">ecpg</code> command is
detailed in <a href="app-ecpg.html" title="ecpg"><span class="refentrytitle"><span class="application">ecpg</span></span></a>.
</p>
<p> The <span class="application">ecpg</span> library is thread-safe if it is built
using the <code class="option">--enable-thread-safety</code> command-line option to
<code class="filename">configure</code>. (You might need to use other threading
command-line options to compile your client code.)
</p>
</div></body>
</html>
|