Building C and C++ routine code using sample bldrtn scripts

Building C and C++ routine source code is a subtask of creating C and C++ routines. You can easily build C and C++ routines with the database sample build scripts (Linux® and UNIX) and batch files (Windows). The sample build scripts can be used for source code with or without embedded SQL statements. The build scripts can pre-compile, compile, and link C or C++ source code. Thee build scripts can also bind any packages that are associated with the embedded SQL routine to the specified database.

Before you begin

The sample build-script for building C and C++ routines is named bldrtn. The bldrtn script is located in the following database directories along with the sample programs:
  • For C:sqllib/samples/c/
  • For C++: sqllib/samples/cpp/
The bldrtn script can be used to build a source code file that contains both procedure and function implementations. The bldrtn script performs the following tasks:
  • 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 database 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 is 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 that contains one or more routine implementations.
  • The name of the database within the current database instance in which the routines are to be created.

Procedure

To build a source code file that contains one or more routine code implementations:

  1. Open the Db2® command window.
  2. Copy your source code file into the same directory as the bldrtn script file.
  3. 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 bldrtn 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.
  4. If this is not the first time that the source code is built, stop and restart the database instance to ensure that the new version of the shared library is used by the database. You can restart the database instance by entering the db2stop command followed by the db2start command.

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.