The following example shows three COBOL programs, one of which (alpha) calls the other two (beta and gamma). The procedure after the programs shows how to create a shared library that contains the two called programs, create an archive library that contains that shared library, and compile and link the calling program into a module that accesses the called programs in the archive library.
IDENTIFICATION DIVISION.
PROGRAM-ID. alpha.
*
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
*
DATA DIVISION.
WORKING-STORAGE SECTION.
01 hello1 pic x(30) value is "message from alpha to beta".
01 hello2 pic x(30) value is "message from alpha to gamma".
*
PROCEDURE DIVISION.
display "alpha begins"
call "beta" using hello1
display "alpha after beta"
call "gamma" using hello2
display "alpha after gamma"
goback.
IDENTIFICATION DIVISION.
PROGRAM-ID. beta.
*
ENVIRONMENT DIVISION.
*
DATA DIVISION.
WORKING-STORAGE SECTION.
*
Linkage section.
01 msg pic x(30).
*
PROCEDURE DIVISION using msg.
DISPLAY "beta gets msg=" msg.
goback.
IDENTIFICATION DIVISION.
PROGRAM-ID. gamma.
*
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
*
DATA DIVISION.
WORKING-STORAGE SECTION.
*
Linkage section.
01 msg pic x(30).
*
PROCEDURE DIVISION using msg.
DISPLAY "gamma gets msg=" msg.
goback.
The simplest way to combine the three programs is to compile and link them into a single executable module by using the following command:
cob2 -o m_abg alpha.cbl beta.cbl gamma.cbl
You can then run the programs by issuing the command m_abg, which results in the following output:
alpha begins beta gets msg=message from alpha to beta alpha after beta gamma gets msg=message from alpha to gamma alpha after gamma
Instead of linking the programs into a single executable module, you can instead put beta and gamma in a shared library (called sh_bg in the following procedure), and compile and link alpha into an executable module that accesses beta and gamma in the shared library. To do so, do these steps:
#! * symbols exported by shared library sh_bg: BETA GAMMA
The symbol names in export file bg.exp shown above are uppercase because the COBOL default is to use uppercase names for external symbols. If you need mixed-case names, use the PGMNAME(MIXED) compiler option.
If you name the export file bg.exp, you must use option -bE:bg.exp when you create the shared library (as shown in the next step).
cob2 -o sh_bg beta.cbl gamma.cbl -bE:bg.exp -bM:SRE -bnoentry
This command provides the following information to the compiler and linker:
If you have several shared objects, however, you might want to put them in an archive library for convenience. To put sh_bg in an archive library called libbg.a, use the following command:
ar -rv libbg.a sh_bg
cob2 -o m_alpha alpha.cbl -L. -lbg cob2 -o m_alpha alpha.cbl -L. libbg.a
where:
You might instead want to specify the full path of the target archive library. For example:
cob2 -o m_alpha alpha.cbl -L/usr/jane/libraries -lbg
You can then run the program by issuing the command m_alpha, which produces the same output as that shown before the steps above.
As an alternative to issuing the commands in the last three steps, you can create a makefile that includes the commands.
Example: creating a makefile for the sample shared library
related tasks
Passing options to the linker