Building C and C++ routine source code is a sub-task of
creating C and C++ routines. This task can be done quickly and easily
using DB2® sample build scripts
(UNIX) and batch files (Windows). The sample build
scripts can be used for source code with or without embedded SQL statements.
The build scripts take care of the pre-compilation, compilation,
and linking of C and C++ source code that would otherwise have to
be done in individual steps from the command line. They also take
care of binding any packages to the specified database.
Before you begin
The sample build scripts for building C and C++ routines
are named
bldrtn. They are located in DB2 directories along with sample
programs that can be built with them as follows:
- For C: sqllib/samples/c/
- For C++: sqllib/samples/cpp/
The
bldrtn script can be used to build
a source code file containing both procedures and function implementations.
The script does the following:
- Establishes a connection with a user-specified database
- Precompiles the user-specified source code file
- Binds the package to the current database
- Compiles and links the source code to generate a shared library
- Copies the shared library to the DB2 function
directory on the database server
The
bldrtn scripts accept two arguments:
- The name of a source code file without any file extension
- The name of a database to which a connection will be established
The database parameter is optional. If no database name
is supplied, the program uses the default sample database. As routines
must be built on the same instance where the database resides, no
arguments are required for a user ID and password.
- Source code file containing one or more routine implementations.
- The name of the database within the current DB2 instance in which the routines are to be
created.
Procedure
To build a source code file that contains one or more
routine code implementations:
- Open a DB2 Command
Window.
- Copy your source code file into the same directory as the bldrtn script
file.
- If the routines will be created in the sample database,
enter the build script name followed by the name of the source code
file without the .sqc or .sqC file extension:
bldrtn file-name
If
the routines will be created in another database, enter the build
script name, the source code file name without any file extension,
and the database name:
bldrtn file-name database-name
The
script precompiles, compiles and links the source code and produces
a shared library. The script then copies the shared library to the
function directory on the database server.
- If this is not the first time that the source code file
containing the routine implementations was built, stop and restart
the database to ensure the new version of the shared library is used
by DB2. You can do this by entering db2stop followed
by db2start on the command line.
What to do next
Once
you have successfully built the routine shared library and deployed
it to the function directory on the database server, you should complete
the steps associated with the task of creating C and C++ routines.
After routine creation is completed you will be able to invoke your
routines.