Building C/C++ applications on Windows

Db2® provides build scripts for compiling and linking Db2 API and embedded SQL C/C++ programs. These are located in the sqllib\samples\c and sqllib\samples\cpp directories, along with sample programs that can be built with these files.

About this task

The batch file, bldapp.bat, contains the commands to build Db2 API and embedded SQL programs. It takes up to four parameters, represented inside the batch file by the variables %1, %2, %3, and %4.

The first parameter, %1, specifies the name of your source file. This is the only required parameter for programs that do not contain embedded SQL. Building embedded SQL programs requires a connection to the database so three additional parameters are also provided: the second parameter, %2, specifies the name of the database to which you want to connect; the third parameter, %3, specifies the user ID for the database, and %4 specifies the password.

For an embedded SQL program, bldapp passes the parameters to the precompile and bind file, embprep.bat. If no database name is supplied, the default sample database is used. The user ID and password parameters are only needed if the instance where the program is built is different from the instance where the database is located.

Procedure

  • Building and running embedded SQL applications

    There are three ways to build the embedded SQL application, tbmod, from the C source file tbmod.sqc in sqllib\samples\c, or from the C++ source file tbmod.sqx in sqllib\samples\cpp:

    • If connecting to the sample database on the same instance, enter:
         bldapp tbmod
    • If connecting to another database on the same instance, also enter the database name:
         bldapp tbmod database
    • If connecting to a database on another instance, also enter the user ID and password of the database instance:
         bldapp tbmod database userid password
    The result is an executable file tbmod.exe.
    There are three ways to run this embedded SQL application:
    • If accessing the sample database on the same instance, enter the executable name:
         tbmod
    • If accessing another database on the same instance, enter the executable name and the database name:
         tbmod database
    • If accessing a database on another instance, enter the executable name, database name, and user ID and password of the database instance:
         tbmod database userid password
  • Building and running multi-threaded applications

    C/C++ multi-threaded applications on Windows need to be compiled with either the -MT or -MD options. The -MT option will link using the static library LIBCMT.LIB, and -MD will link using the dynamic library MSVCRT.LIB. The binary linked with -MD will be smaller but dependent on MSVCRT.DLL, while the binary linked with -MT will be larger but will be self-contained with respect to the runtime.

    The batch file bldmt.bat uses the -MT option to build a multi-threaded program. All other compile and link options are the same as those used by the batch file bldapp.bat to build regular stand-alone applications.

    To build the multi-threaded sample program, dbthrds, from either the samples\c\dbthrds.sqc or samples\cpp\dbthrds.sqx source file, enter:
       bldmt dbthrds
    The result is an executable file, dbthrds.exe.
    There are three ways to run this multi-threaded application:
    • If accessing the sample database on the same instance, simply enter the executable name (without the extension):
         dbthrds
    • If accessing another database on the same instance, enter the executable name and the database name:
         dbthrds database
    • If accessing a database on another instance, enter the executable name, database name, and user ID and password of the database instance:
         dbthrds database userid password

Example

The following examples show you how to build and run Db2 API and embedded SQL applications.

To build the Db2 API non-embedded SQL sample program, cli_info, from either the source file cli_info.c, in sqllib\samples\c, or from the source file cli_info.cxx, in sqllib\samples\cpp, enter:
   bldapp cli_info
The result is an executable file, cli_info.exe. You can run the executable file by entering the executable name (without the extension) on the command line:
   cli_info