Calling between COBOL and C/C++ programs
You can call functions written in C/C++ from COBOL programs and can call COBOL programs from C/C++ functions.
About this task
In an interlanguage application, you can combine 64-bit COBOL programs with 64-bit C/C++ functions, or 32-bit COBOL programs with 32-bit C/C++ functions.
Restriction: You cannot mix 32-bit components and 64-bit
components in an application.
Interlanguage communication between COBOL and C++: In
an interlanguage application that mixes COBOL and C++, follow these
guidelines:
- Specify
extern "C"in function prototypes for COBOL programs that are called from C++, and in C++ functions that are called from COBOL. - In COBOL, use
BY VALUEparameters to match the normal C++ parameter convention. - In C++, use reference parameters
to match the COBOL
BY REFERENCEconvention. - COBOL programs follow the C and C++ link and calling conventions. C and C++ programs that will be dynamically called by COBOL programs must be created as DSOs (dynamic shared objects) by compiling them using the gcc -fPIC option to emit position-independent code that is suitable for dynamic linking, and linking them with the gcc -shared option to produce a shared object that can then be linked with other objects to form an executable.
- C/C++ functions that return void cannot be reliably called by a COBOL program, because COBOL always expects a return value when it executes a CALL, even when there is no RETURNING phrase on that CALL. In this case, it will take that return value and assign it as the value of the RETURN-CODE special register, which is implicitly defined as PICTURE S9(4) USAGE BINARY. This is the correct behavior when a COBOL program calls another COBOL program without the RETURNING phrase, because the called program will always return its RETURN-CODE special register. However, a C void function does not return any value, so this can result in unexpected results in the COBOL program. To work around this, either change the C function to return a signed short value, or create a C wrapper function which calls the original C void function, and then returns a signed short value.
The following rules and guidelines provide further information about how to perform these interlanguage calls.
Unqualified references to "C/C++" in the referenced sections are to GNU GCC compiler.