34.10. Processing Embedded SQL Programs#
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 embeddedSQLC preprocessor, which converts theSQL 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 theSQL command using thelibpq interface, and put the result in the arguments specified for output. The preprocessor program is called This will create a file called The preprocessed file can be compiled normally, for example: The generated C source files include header files from thePostgreSQL installation, so if you installedPostgreSQL in a location that is not searched by default, you have to add an option such as To link an embedded SQL program, you need to include the Again, you might have to add an option like You can use If you manage the build process of a larger project usingmake, it might be convenient to include the following implicit rule to your makefiles: The complete syntax of the Theecpg library is thread-safe by default. However, you might need to use some threading command-line options to compile your client code.ecpg
and is included in a normalPostgreSQL installation. Embedded SQL programs are typically named with an extension.pgc
. If you have a program file calledprog1.pgc
, you can preprocess it by simply calling:ecpg prog1.pgc
prog1.c
. If your input files do not follow the suggested naming pattern, you can specify the output file explicitly using the-o
option.cc -c prog1.c
-I/usr/local/pgsql/include
to the compilation command line.libecpg
library, like so:cc -o myprog prog1.o prog2.o ... -lecpg
-L/usr/local/pgsql/lib
to that command line.pg_config
orpkg-config
with package namelibecpg
to get the paths for your installation.ECPG = ecpg%.c: %.pgc $(ECPG) $<
ecpg
command is detailed inecpg.