]> begriffs open source - ai-pg/blob - full-docs/txt/ecpg-process.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ecpg-process.txt
1
2 34.10. Processing Embedded SQL Programs #
3
4    Now that you have an idea how to form embedded SQL C programs, you
5    probably want to know how to compile them. Before compiling you run the
6    file through the embedded SQL C preprocessor, which converts the SQL
7    statements you used to special function calls. After compiling, you
8    must link with a special library that contains the needed functions.
9    These functions fetch information from the arguments, perform the SQL
10    command using the libpq interface, and put the result in the arguments
11    specified for output.
12
13    The preprocessor program is called ecpg and is included in a normal
14    PostgreSQL installation. Embedded SQL programs are typically named with
15    an extension .pgc. If you have a program file called prog1.pgc, you can
16    preprocess it by simply calling:
17 ecpg prog1.pgc
18
19    This will create a file called prog1.c. If your input files do not
20    follow the suggested naming pattern, you can specify the output file
21    explicitly using the -o option.
22
23    The preprocessed file can be compiled normally, for example:
24 cc -c prog1.c
25
26    The generated C source files include header files from the PostgreSQL
27    installation, so if you installed PostgreSQL in a location that is not
28    searched by default, you have to add an option such as
29    -I/usr/local/pgsql/include to the compilation command line.
30
31    To link an embedded SQL program, you need to include the libecpg
32    library, like so:
33 cc -o myprog prog1.o prog2.o ... -lecpg
34
35    Again, you might have to add an option like -L/usr/local/pgsql/lib to
36    that command line.
37
38    You can use pg_config or pkg-config with package name libecpg to get
39    the paths for your installation.
40
41    If you manage the build process of a larger project using make, it
42    might be convenient to include the following implicit rule to your
43    makefiles:
44 ECPG = ecpg
45
46 %.c: %.pgc
47         $(ECPG) $<
48
49    The complete syntax of the ecpg command is detailed in ecpg.
50
51    The ecpg library is thread-safe by default. However, you might need to
52    use some threading command-line options to compile your client code.