Nonstop Database, HiRDB Version 9 UAP Development Guide
![[Contents]](FIGURE/CONTENT.GIF)
![[Index]](FIGURE/INDEX.GIF)
![[Back]](FIGURE/FRONT.GIF)
10.2.1 Creating an external C stored routine
(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.
- Specify the C language's default call convention (__cdecl).
- For the case where the program terminates normally, set 00000 in the output parameter for SQLSTATE.
Example: memcpy(sqlstate, "00000", 5);
- For the case where the program terminates abnormally, set 38XYY in the output parameter for SQLSTATE, where the values of X and Y must be in the following ranges:
X: I to Z
Y: 0 to 9 or A to Z
Example 1: memcpy(sqlstate, "38I01", 5);
Example 2: memcpy(sqlstate, "38ZCB", 5);
- For the case where the program terminates abnormally, set a message text that indicates the cause of the error.
The following figure shows an example of C program coding.
Figure 10-2 Example C program coding
![[Figure]](FIGURE/ZU100020.GIF)
(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]](FIGURE/ZU100100.GIF)
(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]](FIGURE/ZU100030.GIF)
- 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]](FIGURE/ZU100050.GIF)
- 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:
- Create an object file by using the cc command with the +z option specified.
$ cc -c +z sample.c
- 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:
- Create an object file by using the xlc command.
$ xlc -c -o sample.o sample.c
- 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:
- Create an object file by using the cl command.
cl /MD /c sample.c
- 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.
All Rights Reserved. Copyright (C) 2011, Hitachi, Ltd.