Nonstop Database, HiRDB Version 9 UAP Development Guide

[Contents][Index][Back][Next]

10.2.1 Creating an external C stored routine

To create an external C stored routine:

  1. Code a C program (create a C file).
  2. Compile (create an object file).
  3. Link (create a C library file).
Organization of this subsection
(1) Coding a C program (creating a C file)
(2) Compiling (creating an object file)
(3) Linking (creating a C library file)
(4) Examples of C library file creation for different OSs

(1) Coding a C program (creating a C file)

You code a C program that is to be registered as an external C stored routine.

(a) C program creation rules

This subsection discusses the rules for creating C programs. Also see 10.4 Limitations to C program creation.

The following figure shows an example of C program coding.

Figure 10-2 Example C program coding

[Figure]

(b) Mapping of parameter input/output modes

The following figure shows a sample mapping of SQL parameter input/output modes (IN, OUT, and INOUT) in an external C stored routine. For details about mapping, see type mapping in the manual HiRDB Version 9 SQL Reference.

Figure 10-3 Example of mapping of parameter input/output modes (external C stored routine)

[Figure]

(2) Compiling (creating an object file)

You use a method such as the cc command to create an object file from the C file.

The figure below shows an example of compilation. For details about the compiler options, see the OS documentation.

Figure 10-4 Example of compilation (external C stored routine)

[Figure]

Note
For details about the compiler options, see the applicable OS's documentation.

(3) Linking (creating a C library file)

You use a method such as the ld command to create a C library file from multiple object files. If you use library functions, link the required libraries by using, for example, the -l option.

The following figure shows an example of linking.

Figure 10-5 Example of linking

[Figure]

Note 1
The C library file extension depends on the OS.

Note 2
For details about the linker options, see the documentation for the applicable OS.

(4) Examples of C library file creation for different OSs

This subsection presents examples of C library file creation from a C file (sample.c) for the different OSs. Creation of C library files varies according to the OS. For details, see the applicable OS's documentation.

The examples presented here assume that the compiler and linker are on the specified path.

(a) In HP-UX

Create a C library file named sample.sl from sample.c:

  1. Create an object file by using the cc command with the +z option specified.
    $ cc -c +z sample.c
  2. Create a C library file by using the ld command with the -b option specified.
    $ ld -b -o sample.sl sample.o

In 64-bit mode:
If you use the C library file in a 64-bit-mode HiRDB system, compile the file in 64-bit mode. In this case, specify the +DD64 compiler option.

In the POSIX library edition:
If you use the C library file in a 64-bit-mode HiRDB system, make sure that the following conditions are satisfied, because the file must support multiple threads:
  • Specify the following options during compilation:
    -D_REENTRANT -D_POSIX_C_SOURCE=199506L -D_THREAD_SAFE -D_HPUX
  • Use thread-safe functions.
(b) In Solaris

Create a C library file by using the cc command with the -G option specified. The following example creates a C library file named sample.so from sample.c:

$ cc -G sample.c -o sample.so

In 64-bit mode:
If you use the C library file in a 64-bit-mode HiRDB system, compile the file in 64-bit mode. In this case, specify the -xarch=v9 compiler option.
(c) In Linux

Create a C library file by using the gcc command with the -shared option specified. The following example creates a C library file named sample.so from sample.c:

$ gcc -shared -fPIC -o sample.so sample.c

(d) In AIX

Create a C library file named sample.so from sample.c:

  1. Create an object file by using the xlc command.
    $ xlc -c -o sample.o sample.c
  2. Create a C library file by using the xlc command with the -G option specified.
    $ xlc -G -bexpall -o sample.so sample.o

In 64-bit mode:
If you use the C library file in a 64-bit-mode HiRDB system, compile and link the file in 64-bit mode. In this case, specify the -q64 compiler option and -b64 linker option.

In the POSIX library edition:
If you use the C library file in a 64-bit-mode HiRDB system, make sure that the following conditions are satisfied, because the file must support multiple threads:
  • Compile the C file by using the xlc_r command.
  • Use thread-safe functions.
(e) In Windows

Create a C library file (DLL file) named sample.dll from sample.c:

  1. Create an object file by using the cl command.
    cl /MD /c sample.c
  2. Create a C library file (DLL file) by using the link command. Also create a module definition file (sample.def) and specify the function to be exported.
    link /dll /def:sample.def sample.obj

Notes
  • Do not specify the base address (default load address) of the DLL to be created. If the base address is specified, address contention might occur between the DLLs of HiRDB and of the system, resulting in an overload of DLL loading processing.
  • You must use a multi-thread DLL edition (/MD) of the Microsoft Visual C++ runtime library. If any other library edition is used, area management processing might fail, resulting in abnormal termination of server processes.