Advanced Uses

The cob -z option accepts .int files and system object (.o) files as input, as well as recognized source code programs. Hence, C and C++ source and/or object code can be linked into a callable shared object.

For example, the following command creates myapp.so using the COBOL programs mycobol.cbl and myint.int, the C source code myc.c, and an object file myobj.o. The object file could be COBOL, C, C++ or some other language or third party system object file. The main entry point is specified as myentry which could be in any of the specified modules.

cob -zo myapp.so mycobol.cbl myint.int myc.c myobj.o 
    -e myentry

When creating callable shared objects that contain C++, the -z,CC option should be used so that the C++ compiler creates the final callable shared object. For example, the following command creates myapp.so using the COBOL program mycobol.cbl, the C source code myc.c, and a C++ source file mycpp.C. The main entry point is specified as myentry which could be in any of the specified modules.

cob -z,CC -o myapp.so mycobol.cbl myc.c mycpp.C -e myentry

A shared library can be linked into a callable shared library using the -l and -L cob options. The shared library will be loaded when the callable shared object is loaded and unloaded when the callable shared object is unloaded. For example, the following command creates a shared library libmylib.so and then links it into myapp.so. When myapp.so is loaded at run time the operating system searches for and loads the shared library libmylib.so.

cob -Z mylib.cbl myobj.o
cob -zo myapp.so myprog.cbl myc.c -L. -lmylib -e myentry

A callable shared object does not need to contain COBOL and so could contain just C or C++ or objects supplied by a third party.

C, C++ or third party objects can be linked into callable shared objects or libraries. For example, you can now code your application to cope with situations in which a third party product is not installed on a machine. This is achieved by linking the third party objects and shared libraries into a callable shared object, not a system executable. You then load this callable shared object, when required, using the COBOL ON EXCEPTION syntax. If the third party product is not installed then the callable shared object will not load and the ON EXCEPTION route of your code will be taken.