Building C and C++ routine code from the Db2 command window

You can build C and C++ routines from the command line. The procedure to build C and C++ routines include compilation and linking of the source code, followed by deploying the routine library. If you have any embedded SQL statements present in the source code, you must first run the precompiler before you can proceed with the C or C++ routine compilation. You must also bind the resulting bind file that is generated by the precompiler.

Before you begin

As an alternative to manually building the C and C++ routines, you can modify the sample build scripts that are provided in the sample subdirectory of the sqllib directory to simplify this task.

The following conditions must be met before you can build the C and C++ source code with embedded SQL statements:
  • You must have a source code file that contains one or more embedded SQL C or C++ routine implementations.
  • You must have the database information in which the routines are to be created.
  • You must have compiler options that are operating system specific and link options that are required for building C and C++ routines.

Procedure

To build a source code file that contains one or more embedded SQL statements:

  1. Open the Db2 command window.
  2. Navigate to the directory that contains your source code file.
  3. Establish a connection with the database in which the routines are created.
  4. Precompile the source code file.
  5. Bind the package that was generated to the database.
  6. Compile the source code file.
  7. Link the source code file to generate a shared library. The linking process requires the use of compiler link options that references the database include directory.
  8. Copy the shared library to the database function directory on the database server.
  9. If you are not building the source code file for the first time, you must stop and restart the database manager to ensure that the new version of the shared library is used by the database manager. You can issue the db2stop command followed by the db2start command to restart the database manager.

Results

Once you successfully built and deployed the routine library, you can register the new routine in the database by running the CREATE statement. The routine must be registered before you can call the new routine.

Example

The following example builds an embedded SQL C++ source code file that is named myfile.sqC, which contains a routine implementation. The routine is compiled with IBM® C and C++ compiler on the AIX® operating system.

  1. Open the Db2 command window.

  2. Navigate to the directory that contains your source code file.

  3. Establish a connection with the database to which the routine is intended for.
       db2 connect to database-name
  4. Precompile the source code file with the PREPARE or PREP command.
       db2 prep myfile.sqC bindfile

    The precompiler displays an output to indicate whether there were any errors. The precompile step generates a bind file that is named myfile.bnd, which can be used to generate a package in the next step.

  5. Bind the package that was generated to the database with the BIND command.
       db2 bind myfile.bnd

    The bind utility displays an output to indicate whether there were any errors.

  6. Compile the source code file and specify any compiler options that you need to specify. You must include the database include directory.
       xlC_r -qstaticinline -I$HOME/sqllib/include -c $myfile.C

    The compiler displays an output to indicate whether there are any errors. This step generates an export file named myfile.exp.

  7. Link the compiled object to generate a shared library.
       xlC_r -qmkshrobj -o $1 $1.o -L$ HOME/sqllib/include/lib32 -lDB2

    The linker displays an output to indicate whether there are any errors. This step generates a shared library file name myfile.

  8. Copy the shared library to the database function directory on the database server.
       rm -f ~HOME/sqllib/function/myfile
       cp myfile $HOME/sqllib/function/myfile

    This step ensures that the routine library is in the default directory where the database manager looks for routine libraries.

  9. Stop and restart the database manager.
       db2stop
       db2start

The LD_LIBRARY_PATH operating system environment variable is ignored when a source code with embedded SQL statements is built.