Building C/C++ routines on Windows

You can use the provided build scripts for compiling and linking Db2® API and embedded SQL programs in C and C++. The scripts are in the sqllib\samples\c and sqllib\samples\cpp directories. The directories also contain sample programs that you can build with these files. You can find the commands to build embedded SQL routines, such as stored procedures and user-defined functions, in the bldrtn.bat batch file.

About this task

The batch file builds a DLL on the server. It takes two parameters, represented inside the batch file by the variables %1 and %2.

The first parameter, %1, specifies the name of your source file. The batch file uses the source file name for the DLL name. The second parameter, %2, specifies the name of the database to which you want to connect. Since the DLL must be built on the same instance where the database resides, there are no parameters for user ID and password.

Only the first parameter, the source file name, is required. Database name is optional. If no database name is supplied, the program uses the default sample database.

The following examples show you how to build routine DLLs with:

  • stored procedures
  • non-embedded SQL user-defined functions (UDFs)
  • embedded SQL user-defined functions (UDFs)
Stored procedure DLL
To build the spserver DLL from either the C source file, spserver.sqc, or the C++ source file, spserver.sqx:
  1. Enter the batch file name and program name:
       bldrtn spserver
    If connecting to another database, also enter the database name:
       bldrtn spserver database
    The batch file uses the module definition file spserver.def, contained in the same directory as the sample programs, to build the DLL. The batch file copies the DLL, spserver.dll, to the server in the path sqllib\function.
  2. Next, catalog the routines by running the spcat script on the server:
       spcat
    This script connects to the sample database, uncatalogs the routines if they were previously cataloged by calling spdrop.db2, then catalogs them by calling spcreate.db2, and finally disconnects from the database. You can also call the spdrop.db2 and spcreate.db2 scripts individually.
  3. Then, stop and restart the database to allow the new DLL to be recognized. If necessary, set the file mode for the DLL, so that the Db2 instance can access it.

Once you build the DLL, spserver, you can build the client application spclient that calls it.

You can build spclient by using the batch file, bldapp.bat.

To call the DLL, run the sample client application by entering:

spclient database userid password

where
database
Is the name of the database to which you want to connect. The name could be sample, or its alias, or another database name.
userid
Is a valid user ID.
password
Is a valid password for the user ID.

The client application accesses the DLL, spserver, and executes a number of routines on the server database. The output is returned to the client application.

Non-embedded SQL UDF DLL
To build the user-defined function udfsrv from the source file udfsrv.c, enter:
   bldrtn udfsrv

The batch file uses the module definition file, udfsrv.def, contained in the same directory as the sample program files, to build the user-defined function DLL. The batch file copies the user-defined function DLL, udfsrv.dll, to the server in the path sqllib\function.

Once you build udfsrv, you can build the client application, udfcli, that calls it. CLI, as well as embedded SQL C and C++ versions of this program are provided.

You can build the CLI udfcli program from the udfcli.c source file in sqllib\samples\cli using the batch file bldapp.

You can build the embedded SQL C udfcli program from the udfcli.sqc source file in sqllib\samples\c using the batch file bldapp.

You can build the embedded SQL C++ udfcli program from the udfcli.sqx source file in sqllib\samples\cpp using the batch file bldapp.

To run the UDF, enter:
   udfcli

The calling application calls the ScalarUDF function from the udfsrv DLL.

Embedded SQL UDF DLL
To build the embedded SQL user-defined function library udfemsrv from the C source file udfemsrv.sqc in sqllib\samples\c, or from the C++ source file udfemsrv.sqx in sqllib\samples\cpp, enter:
   bldrtn udfemsrv
If connecting to another database, also enter the database name:
   bldrtn udfemsrv database

The batch file uses the module definition file, udfemsrv.def, contained in the same directory as the sample programs, to build the user-defined function DLL. The batch file copies the user-defined function DLL, udfemsrv.dll, to the server in the path sqllib\function.

Once you build udfemsrv, you can build the client application, udfemcli, that calls it. You can build udfemcli from the C source file udfemcli.sqc in sqllib\samples\c, or from the C++ source file udfemcli.sqx in sqllib\samples\cpp using the batch file bldapp.

To run the UDF, enter:
   udfemcli

The calling application calls the UDFs in the udfemsrv DLL.