Next: , Previous: Multiple Sources, Up: Multiple Sources


2.2.1 Static Linking

The easiest way of combining multiple files is to compile them into a single executable.

One way is to specify all files on the command line:

     $ cobc -x -o prog main.cob subr1.cob subr2.cob

Another way is to compile each file with the option -c, and link them at the end. The top-level program must be compiled with the option -x:

     $ cobc -c subr1.cob
     $ cobc -c subr2.cob
     $ cobc -c -x main.cob
     $ cobc -x -o prog main.o subr1.o subr2.o

You can link C routines as well using either method:

Method 1:

     $ cobc -o prog main.cob subrs.c

Method 2:

     $ cc -c subrs.c
     $ cobc -c -x main.cob
     $ cobc -x -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.