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:
subr(X);
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.