Multiple Sources †
A program often consists of multiple source files. This section describes how to compile multiple source files.
This section also describes how to build a shared library that can be used by any COBOL programs and how to use external libraries from COBOL programs.
Static Linking †
The easiest way of combining multiple files is to compile them into a single executable.
Compile each file with the option -c, and link them at the end. The top-level program must be compiled with the option -fmain:
$ cobc -c subr1.cob $ cobc -c subr2.cob $ cobc -c -fmain main.cob $ cobc -o prog main.o subr1.o subr2.o
You can link C routines as well:
$ cc -c subrs.c $ cobc -c -fmain main.cob $ cobc -o prog main.o subrs.o
Any number of functions can be contained in a single C file.
The linked programs will be called dynamically; that is, the symbol will be resolved at run time. For example, the following COBOL statement
CALL "subr" USING X.
will be converted into an equivalent C code like this:
int (*func)() = cob_resolve("subr"); if (func != NULL) func (X);
With the compiler options -fstatic-call, more efficient code will be generated like this:
Note that this option is effective only when the called program name is a literal (like CALL "subr".). With a data name (like CALL SUBR.), the program is still called dynamically.
Dynamic Linking †
The main program and subprograms can be compiled separately.
The main program is compiled as usual:
$ cobc -x main.cob
Subprograms are compiled with the option -m:
$ cobc -m subr.cob
This creates a module file subr.so (The extension varies depending on your host.).
Before running the main program, install the module files in your library directory:
$ cp subr.so /your/cobol/lib
Now, set the environment variable COB_LIBRARY_PATH to your library directory, and run the main program:
$ export COB_LIBRARY_PATH=/your/cobol/lib $ ./main
Building Library †
You can build a shared library by combining multiple COBOL programs and even C routines:
$ cobc -c subr1.cob $ cobc -c subr2.cob $ cc -c subr3.c $ cc -shared -o libsubrs.so subr1.o subr2.o subr3.o
Using Library †
You can use a shared library by linking it with your main program.
Before linking the library, install it in your system library directory:
$ cp libsubrs.so /usr/lib
or install it somewhere else and set LD_LIBRARY_PATH:
$ cp libsubrs.so /your/cobol/lib $ export LD_LIBRARY_PATH=/your/cobol/lib
Then, compile the main program, linking the library as follows:
$ cobc main.cob -L/your/cobol/lib -lsubrs
Last-modified: Tue, 15 Jan 2008 05:13:49 GMT (2532d)