A COBOL module for C

The previous lesson put the COBOL object inside the C executable and the linker decided everything. The other arrangement keeps the COBOL in a file of its own: the C program asks the runtime for a module by name at run time and calls the pointer it gets. The module can be rebuilt without the driver and the driver without the module.

cobc -m and cob_resolve

The module is the subprogram of the previous lesson, in a file that also holds a self-test main so that the listing can be built and run on its own. The main calls the subprogram once with a known amount; the subprogram is the entry point the C driver asks for by name:

IDENTIFICATION DIVISION. PROGRAM-ID. TAXSELFTEST. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-AMOUNT PIC S9(9) USAGE COMP-5. 01 WS-RESULT PIC S9(9) USAGE COMP-5. 01 WS-A-OUT PIC Z(8)9. 01 WS-R-OUT PIC Z(8)9. PROCEDURE DIVISION. MOVE 300 TO WS-AMOUNT CALL "TAXCALC" USING WS-AMOUNT WS-RESULT MOVE WS-AMOUNT TO WS-A-OUT MOVE WS-RESULT TO WS-R-OUT DISPLAY "tax " FUNCTION TRIM(WS-R-OUT) " of " FUNCTION TRIM(WS-A-OUT) STOP RUN. END PROGRAM TAXSELFTEST. IDENTIFICATION DIVISION. PROGRAM-ID. TAXCALC. DATA DIVISION. LINKAGE SECTION. 01 L-AMOUNT PIC S9(9) USAGE COMP-5. 01 L-RESULT PIC S9(9) USAGE COMP-5. PROCEDURE DIVISION USING L-AMOUNT L-RESULT. COMPUTE L-RESULT = L-AMOUNT * 13 / 100 GOBACK. END PROGRAM TAXCALC.

Built with -x, the file runs its own test. Built with -m and an output name, it becomes a shared object that keeps both entry points, and the C side resolves the one it wants:

cobc -x -free -Wall -o taxtest callee.cob
./taxtest

tax 39 of 300

The driver asks cob_resolve for the entry point and checks the pointer before it calls it:

#include <stdio.h> #include <libcob.h> typedef int (*cobol_fn)(void *, void *); int main(int argc, char **argv) { int amount = 300; int result = 0; cobol_fn tax; cob_init(argc, argv); tax = (cobol_fn)cob_resolve("TAXCALC"); if (tax == NULL) { printf("not found: %s\n", cob_resolve_error()); return 1; } tax(&amount, &result); printf("tax %d of %d\n", result, amount); return 0; }

cobc -m -free -Wall -o TAXCALC.so callee.cob
cc -o resolve resolve.c -lcob -lm
./resolve
echo $?

tax 39 of 300
0

The link line has no COBOL object in it this time, and it needs none: the driver was built without the module and found it at run time, in the folder of the executable.

A module that is not there

cob_resolve answers a null pointer for a name it cannot find, and cob_resolve_error answers the reason. The same driver without the module beside it:

mkdir -p other
mv TAXCALC.so other/
./resolve
echo $?

not found: module 'TAXCALC' not found
1

The driver printed the message and returned 1, which is the honest answer for a program whose work cannot start. A driver that called the pointer without the check would crash on a null function pointer, and the crash would say nothing about the missing file.

COB_LIBRARY_PATH

The search path of the runtime is the variable the COBOL side uses for the same purpose:

COB_LIBRARY_PATH=other ./resolve

tax 39 of 300

The module stayed in the subfolder and the driver found it, so a shop keeps one copy of its common COBOL modules in a directory of their own and sets the variable in the job that starts the program. The lesson on modules and objects showed the same variable for a COBOL caller.

A module against an object

The two builds of the same source, side by side, with the sizes this container reported:

FileSizeWhere the COBOL is
TAXCALC.so19384in the module
resolve18528nowhere: the driver only asks for it
callee.o14216in the object
driver of the previous lesson21544inside the executable

The driver that links the object is 3016 bytes larger than the one that resolves a module, and the module is a file of its own that must be shipped beside it. The trade is the one the module lesson described from the COBOL side: a module is rebuilt once and every caller picks it up at its next run, an object needs every executable to be linked again.

The next lesson does the same from Python, where the module is loaded with ctypes and no C file is written at all.

Exercises

  1. Rebuild the module with -O2 and compare its size with the one in the table.
  2. Change the name in cob_resolve to a program that does not exist and read the message.
  3. Put the module in two folders and set COB_LIBRARY_PATH to the second. Which copy is loaded?
  4. Build the same subprogram with -c and link it into the driver instead of resolving it. Which file does the driver then need at run time?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does cobc -m build?

    Answer

    A shared module that the runtime loads by name

    The driver resolved TAXCALC from TAXCALC.so at run time.

  2. What does cob_resolve answer for a name it cannot find?

    Answer

    A null pointer, with the reason in cob_resolve_error

    The driver printed not found: module TAXCALC not found and returned 1.

  3. Which variable tells the runtime where to look for modules?

    Answer

    COB_LIBRARY_PATH

    With COB_LIBRARY_PATH=other the driver found the module in a subfolder.

  4. Which build puts the COBOL inside the C executable?

    Answer

    Linking the object file of the subprogram

    The linked driver is larger and needs no module at run time.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC S9(9) USAGE COMP-5 VALUE 300. 01 W-R PIC S9(9) USAGE COMP-5. PROCEDURE DIVISION. COMPUTE W-R = W-A * 13 / 100 DISPLAY W-R. STOP RUN.

    Answer

    +0000000039

    Thirteen per cent of three hundred is thirty-nine.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9(2) VALUE 0. 01 W-I PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM VARYING W-I FROM 1 BY 1 UNTIL W-I IS GREATER THAN 3 ADD 1 TO W-N END-PERFORM. DISPLAY W-N. STOP RUN.

    Answer

    03

    Three passes count three.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC S9(4) USAGE COMP-5 VALUE 1000. 01 W-B PIC S9(4) USAGE COMP-5 VALUE 30. 01 W-R PIC S9(9) USAGE COMP-5. PROCEDURE DIVISION. COMPUTE W-R = W-A * W-B / 100 DISPLAY W-R. STOP RUN.

    Answer

    +0000000300

    Thirty per cent of a thousand is three hundred.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-O PIC X(6) VALUE "MODULE". 01 W-L PIC 9(2). PROCEDURE DIVISION. COMPUTE W-L = FUNCTION STORED-CHAR-LENGTH(W-O) DISPLAY W-L. STOP RUN.

    Answer

    06

    The field is six characters wide and all six are in use.

  9. Why does the driver check the pointer before it calls it?

    Answer

    Calling a null pointer crashes and says nothing about the missing module

    The driver printed the reason and returned 1 instead of crashing.

  10. What has to be shipped with a driver that resolves a module?

    Answer

    The module file, in the folder of the driver or on COB_LIBRARY_PATH

    Without TAXCALC.so the driver stopped before its first call.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and gcc 15.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image