![]() |
|
To create an executable C program from an ESQL/C source file, use the esql command. The Informix installation script installs the esql command as part of the ESQL/C product. This section describes what the esql command can do and how you use it.
The esql command performs the following steps:
You can use the esql command to perform all these steps. For more information, see Compiling an INFORMIX-ESQL/C Program.
Before you use esql, make sure that:
The following sections describe the syntax available for the esql command. This section organizes the command-line options by the processing phase that they affect:
The following diagram gives the esql command options for the UNIX operating system. For more information on the -cp, -np, -nup, and -cponly options, see Running the C Preprocessor Before the ESQL/C Preprocessor.
The following diagram shows the general syntax of the ESQL/C command processor, esql. For more information on the -cp, -np, -nup, and onlycp options, see Running the C Preprocessor Before the ESQL/C Preprocessor.
An ESQL/C program must be preprocessed before a C compiler can compile it. The ESQL/C preprocessor converts the embedded SQL statements to C language code.
You can use all the preprocessor options that the following sections describe for preprocessing only or for preprocessing, compiling, and linking.
Use the -V option to obtain the Informix version number and serial number for your ESQL/C product, as the following example shows:
Most preprocessor options that precede a filename affect the file. Suppose you issue the following command:
The -G option affects the demo1.ec file, and both the -ansi and the -G options affect the demo2.ec file.
An exception is the -o option, which names the executable file for all of the source files that follow it on the command line. For example, the executable file inpt is created from both of the source files, custinpt.ec and ordinpt.ec, on the following command line.
By default, the esql command causes the ESQL/C program to be preprocessed, compiled, and linked. The output of the esql command is an executable file. You can specify the -e option to suppress the compilation and linking of your ESQL/C program. With this option, esql only causes preprocessing of the code. The output of this command is a C source file (.c extension).
For example, to preprocess the program that resides in the file demo1.ec, you use the following command:
The preceding command would generate a C source file that is called demo1.c. The following esql command preprocesses demo1.ec, checks for Informix extensions to ANSI-standard syntax, and does not use line numbers:
You can use the -thread option to instruct the preprocessor to generate thread-safe code.
You must use the THREADLIB environment variable with this option to specify which thread package to use when you compile your application.
For Windows environments, the Informix general libraries (libgen, libos, libgls, libafs, and libsql) are shared, thread-safe DLLs. Therefore, the esql command links the shared, thread-safe DLLs automatically. You do not set the THREADLIB environment variable when you compile multithreaded applications in a Windows environment.
When you compile an ESQL/C program, you can instruct the preprocessor to check for Informix extensions to ANSI-standard SQL syntax in one of two ways:
With the -ansi option, the ESQL/C preprocessor generates a warning message when it encounters an Informix extension to ANSI SQL syntax. The following esql command preprocesses, compiles, and links the demo1.ec program and verifies that it does not contain any Informix extensions to the ANSI-standard syntax:
If you compile a program with both the -ansi and -xopen options, the ESQL/C preprocessor generates warning messages for Informix extensions to both ANSI and X/Open SQL syntax.
You can use the -ED and -EU options to create or remove definitions during ESQL/C preprocessing.
To create a global definition, use one of the following forms of the -ED option:
The -EDname is equivalent to the define preprocessor directive ($define or EXEC SQL define) with name at the top of your ESQL/C program.
To remove or undefine a definition globally for the entire source file, use the following syntax for the -EU option:
The -EU option has a global effect over the whole file, regardless of other define directives for name.
Important: Do not put a space between ED or EU and the symbol name.
As with the define and undef statements, the ESQL/C preprocessor processes the -ED and -EU options in stage 1 of preprocessing (before it preprocesses the code in your source file). For more information on preprocessor definitions, see The define and undef Directives.
Figure 2-2 shows a code fragment that uses conditional compilation (the ifdef and ifndef directives).
For the code fragment shown in Figure 2-2, the following esql command line does not generate code because the command line undefines the ENABLE_CODE definition for the entire source file:
If you include the -icheck option, the ESQL/C preprocessor generates code in your program that returns a runtime error if an SQL statement returns a null value to a host variable that does not have an associated indicator variable. For example, the following command tells the preprocessor to insert code that checks for null values into the demo1.ec program:
If you do not use the -icheck option, ESQL/C does not generate an error if the database server passes a null value to a host variable without an indicator variable. For more information on indicator variables, see Using Indicator Variables.
The -I preprocessor option allows you to name a directory where the preprocessor searches for ESQL/C and C include files. This option is valid for both the ESQL/C and the C preprocessors as follows:
The preprocessor passes the -I option to the C compiler for processing of C include files (those that a #include preprocessor statement specifies). The syntax for the -I option is as follows:
The directory can reside on a mounted remote file system if the standard C library functions fopen(), fread(), and fclose() can access them.
The following esql command names the UNIX directory /usr/johnd/incls as a directory to search for ESQL/C and C include files within the demo1 program:
Each -I option lists a single directory. To list several directories, you must list multiple -I options on the command line.
To search in both the C:\dorrie\incl and C:\johnd\incls directories in a Windows environment, you would need to issue the following command:
When the preprocessor reaches an include directive, it looks through a search path for the file to include. It searches directories in this sequence:
For more information on ESQL/C include files, see The include Directive.
By default, the ESQL/C preprocessor gives line numbers to the embedded SQL lines in your program. You can change this default numbering with two command-line options:
By default, ESQL/C defines cursor names and statement IDs as global entities. If you use the -local option, static cursor names and static statement IDs that you declare in a file are local to that file. To create the local name, ESQL/C adds a unique tag (two to nine characters long) to the cursor names and statement IDs in an ESQL/C program. If the combined length of the cursor name (or statement ID) and the unique tag exceeds 128 characters, you receive a warning message.
Tip: Informix provides the -local option primarily for backward compatibility with applications that were created in previous versions of ESQL/C. Do not use this option when you compile new applications. Do not mix files compiled with and without the -local flag. If you mix them, you might receive unpredictable results.
If you use the -local option, you must recompile the source files every time you rename them.
By default, esql directs error and warning messages it generates to standard output. If you want the errors and warnings to be put into a file, use the -log option with the filename. For example, the following esql command compiles the program demo1.ec and sends the errors to the err.out file:
This option only affects the error warnings that the ESQL/C preprocessor generates. The warnings from the compile and link stages still go to the standard error output, which is stderr on UNIX, for example.
By default, the preprocessor generates warning messages when it processes an ESQL/C file. To suppress these warning messages, use the -nowarn option. This option has no effect on error messages.
The -xopen option tells the ESQL/C preprocessor that your program uses X/Open standards. When you specify this option, the preprocessor performs the following two tasks:
If you use X/Open SQL in an ESQL/C program, you must recompile any other source files in the same application with the -xopen option.
If you compile a program with both the -xopen and -ansi options, the ESQL/C preprocessor generates warning messages for Informix extensions to both X/Open and ANSI SQL syntax.
The compilation of an ESQL/C source file can follow either the default order, where the ESQL/C preprocessor runs first on the source file, or it can allow the C preprocessor to run on the source file before the ESQL/C preprocessor.
The default sequence of compilation for an ESQL/C source file is as follows:
This default order of compilation is restrictive because you cannot use #defines or typedefs defined in C system header files or custom C header files to define ESQL/C host variables and constants, nor can you use them for conditional compilation of ESQL/C code. With the default order of compilation, C header files do not get included into the ESQL/C source code until after the ESQL/C preprocessor has run, making these definitions unavailable to the ESQL/C preprocessor.
You can make the C preprocessor run on an ESQL/C source file first, thus expanding any C-dependent typedefs or #defines inside the ESQL/C source file before the ESQL/C preprocessor is run on that source file. You can do this in any of the following ways:
The CPFIRST environment variable specifies whether the C preprocessor should run before the ESQL/C preprocessor on all ESQL/C source files. Set the environment variable to TRUE (uppercase only) to run the C preprocessor on all ESQL/C source files, irrespective of whether the -cp option is passed to the esql command, and irrespective of whether the source file has the .ec or the .ecp extension.
The following diagram shows how to set the CPFIRST environment variable in a UNIX C-shell environment.
The following diagram shows how to set the CPFIRST environment variable in a Windows command-line environment.
When a user chooses to run the C preprocessor on an ESQL/C source file before the ESQL/C preprocessor, the file undergoes the following order of compilation.
For details on the phases of SQL keyword protection and unprotection, see SQL Keyword Protection.
When the C preprocessor runs on a source file, it expands the contents of all C header files where they are included within the source file. This expansion makes it possible to use host variables in ESQL/C source files based on types and #define and typedef statements in C header files. The examples given here indicate some of the advantages of sharing header files. In the example in Figure 2-3, the same C header file is used by both ESQL/C and C source files.
In the example in Figure 2-4, the ESQL/C source file contains a host variable based on a type defined in the time.h system-header file.
A C header file can be included anywhere inside an ESQL/C source file. However, to define host variables in ESQL/C files based on #defines and typedefs defined in C header files, you must include the C header file within the EXEC SQL declare section.
Contrast the example in Figure 2-5 on page 2-32, which leads to error -33051: Syntax error on identifier or symbol `name_hostvar with the example in Figure 2-3 on page 2-30 which does not. The only difference is that in the example in Figure 2-3, the C header file with the #define and the typedef that is used in the EXEC SQL declare section is included within that declare section.
|
Figure 2-5 ESQL/C Excerpt That Defines a Host Variable Based on a C Header File Included Outside the Declare Section
|
When the ESQL/C preprocessor runs on a file, it expands all the contents of header files inside the ESQL/C source file where the header file was included in the source file. Therefore, one consequence of including C header files inside the EXEC SQL declare section is that all types of C declaration syntax are included in the EXEC SQL declare section after the pass through the C preprocessor. You can now include all valid C declaration syntax in the EXEC SQL declare section in the EXEC SQL declare section. However, you can only declare host variables based on certain types described in Host-Variable Data Types.
If the ESQL/C preprocessor has problems with certain statements in C header files, you can exclude specific lines from the preprocessing that the ESQL/C preprocessor performs as shown in the example in Figure 2-6.
If the code in the ESQL/C files is passed unprotected to the C preprocessor before it is passed to the ESQL/C preprocessor, certain SQL keywords might be analyzed by the C preprocessor, which causes unintended results. In the following example, the SQL keyword NULL is replaced by the C preprocessor with the value zero, which creates a valid SQL statement, but one which inserts a value into the orders table other than the value that the programmer intended:
When a user gives the option to run the C preprocessor before the ESQL/C preprocessor, the utility eprotect runs before the C preprocessor runs on the ESQL/C source file. The eprotect utility prepends any SQL keyword that occurs in an SQL statement with the prefix SQLKEYWORD_. This prefix is affixed on all SQL keywords inside SQL statements that begin with the EXEC SQL directive and end with a semicolon. When the ESQL/C source file that contains the select statement mentioned earlier is passed to the C preprocessor, the SELECT statement has the following form:
After the C preprocessor runs on an ESQL/C source file, the esql command runs the eprotect utility with the -u mode, which removes all the SQLKEYWORD_ prefixes before it runs the ESQL/C preprocessor on the output of the C preprocessor.
All SQL statements within ESQL/C source files can either begin with the EXEC SQL key words or with the $ prefix. All of the following pairs of statements are equivalent:
However, the $ symbol can also occur at the beginning of typedef definitions such as in the following example:
In cases such as the preceding typedef example, program logic might require that the C preprocessor substitute the value zero in the place of the keyword NULL. Not allowing the C preprocessor to make value substitutions in such cases would lead to errors. Therefore the eprotect utility does not prepend the SQLKEYWORD_ prefix on SQL keywords that appear in SQL statements that begin with the dollar sign ($) symbol.
Important: If you want to run the C preprocessor on your ESQL/C source file before the ESQL/C preprocessor, and if you do not want the C preprocessor to substitute values for the SQL keywords in SQL statements that occur in your source file, you must begin each SQL statement with the keywords EXEC SQL, and not with the dollar sign ($) symbol.
The following additional preprocessing options are available to you if you use ESQL/C in a Windows environment.
The ESQL/C preprocessor translates one embedded SQL statement as one C line. Long lines can cause problems for some debuggers and editors. You can use the -lw option to tell the preprocessor to wrap output lines at a specific column position. For example, the following esql command tells the preprocessor to wrap lines at column 75:
If you omit the -lw option, the preprocessor does not perform line wrapping.
By default, the ESQL/C preprocessor generates error and warning messages when it processes an ESQL/C file. It displays these errors and warnings in the console window. You can change the display of error and warning messages with the following command-line options:
By default, the ESQL/C preprocessor formats the C source file with tab stops at every eighth column position. You can use the -ts option to set different tab stops. For example, the following esql command tells the preprocessor to set tab stops every four characters: