Linking code by using a COBOL static link

You can link the generated COBOL program by adding a static link to each program that calls it.

About this task

Using a static link you can compile, link, and then run the generated COBOL code inside the calling program, but you must statically link it with every program that calls it. If the generated program changes, you must relink it to all its calling programs for the changes to take effect.

You link the generated COBOL program into the calling program using a CALL statement.

Procedure

  1. Declare the generated COBOL program as a constant in the WORKING-STORAGE section of the calling program. For example:

    01 GENERATED-PROG PIC X(8) VALUE 'MINICBL'

    Where MINICBL is the name of the generated COBOL program file.

  2. Add a CALL statement to the PROCEDURE DIVISION of the calling program that uses this constant. For example:

    CALL GENERATED-PROG USING BORROWER LOAN

    You reference each Top Level data item with a USING clause.

  3. Compile the calling program.

Results

The rules are now ready to be executed.

Example static link to a batch program

The following code extract shows how to link the generated COBOL code to a COBOL calling program, using a CALL statement in the PROCEDURE DIVISION. The XOM is based on a COBOL copybook named miniloan that has two Top Level data items: LOAN and BORROWER. These items translate into two classes in the BOM, named Loan and Borrower. The generated COBOL program file is named MINICBL.

IDENTIFICATION DIVISION.
 PROGRAM-ID. MAIN.

DATA DIVISION.
 WORKING-STORAGE SECTION.
    COPY MINILOAN.
01 TABLEINDEX                PIC 999.
01 GENERATED-PROGRAM         PIC X(8) VALUE "MINICBL".

PROCEDURE DIVISION.
ROOT.
    MOVE "JOHN" TO NAME OF BORROWER
    MOVE 100 TO CREDITSCORE OF BORROWER
    MOVE 10000 TO YEARLYINCOME OF BORROWER
    MOVE 10000 TO AMOUNT OF LOAN
    MOVE 5 to YEARLYINTERESTRATE OF LOAN
    MOVE 1000 TO YEARLYREPAYMENT OF LOAN
    MOVE "N" TO APPROVED OF LOAN
    MOVE 0 TO MESSAGECOUNT OF LOAN

    CALL GENERATED-PROGRAM USING LOAN BORROWER

    IF APPROVED OF LOAN = "Y"
        DISPLAY "Loan approved"
    ELSE
        DISPLAY "Loan not approved"
        MOVE 1 TO TABLEINDEX
        PERFORM UNTIL TABLEINDEX > MESSAGECOUNT OF LOAN
            DISPLAY MESSAGES OF LOAN (TABLEINDEX)
            COMPUTE TABLEINDEX = TABLEINDEX + 1
        END-PERFORM
    END-IF
    GOBACK.