cobc options

cobc is a compiler driver: it turns COBOL into C, calls a C compiler on that C, and links the result. Its options are the options of that pipeline, and this lesson walks the ones that change what comes out, with the size or the output each one produced here.

-x, -c and -m

The same program, built three ways. It is a loop of five million additions and a call to a string function, and it prints its total:

IDENTIFICATION DIVISION. PROGRAM-ID. OPT. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-N PIC 9(9) VALUE 0. 01 WS-I PIC 9(9) VALUE 0. 01 WS-T PIC 9(18) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I IS GREATER THAN 5000000 ADD WS-I TO WS-T END-PERFORM. DISPLAY "total " WS-T. DISPLAY FUNCTION TRIM("abc"). STOP RUN.

cobc -x -free -Wall -o opt0 opt.cob
cobc -c -free -Wall -o opt.o opt.cob
cobc -m -free -Wall -o OPT.so opt.cob
ls -l opt0 opt.o OPT.so

-rwxr-xr-x 1 agent agent 19832 Nov 18 11:20 opt0
-rw-r--r-- 1 agent agent 6912 Nov 18 11:20 opt.o
-rwxr-xr-x 1 agent agent 17760 Nov 18 11:20 OPT.so

The executable is 19832 bytes, its object 6912 and the module 17760. The three were explained in the lesson on modules and objects; what matters here is that one source file and one flag produce three different artefacts, and that the object is the small one because it has no runtime and no entry code of its own.

The program itself prints two lines, which is what any of the three builds does when it is run as a program:

./opt0

total 000012500002500000
abc

The total is the sum of the numbers from one to five million, 12500002500000, printed in an eighteen-digit field.

-O and -O2

-O and -O2 go to the C compiler that cobc runs. The file gets smaller and the loop does not get faster, because the arithmetic of this program is not done by the generated C:

cobc -x -free -Wall -O2 -o opt2 opt.cob
ls -l opt0 opt2
for i in 1 2 3; do /usr/bin/time -f "plain %e" ./opt0 > /dev/null; done
for i in 1 2 3; do /usr/bin/time -f "O2 %e" ./opt2 > /dev/null; done

-rwxr-xr-x 1 agent agent 19832 Nov 18 11:20 opt0
-rwxr-xr-x 1 agent agent 14288 Nov 18 11:20 opt2
plain 3.60
plain 3.90
plain 4.09
O2 3.51
O2 4.13
O2 4.10

Twenty-eight per cent smaller and no faster at all: the three runs of each build overlap, and the fastest run is the one of the plain build. The loop adds a PIC 9(9) field to a PIC 9(18) one, which is decimal arithmetic on fifteen to eighteen digits, and in this compiler that arithmetic lives in the runtime library and not in the C that -O2 optimises.

That is a result worth remembering before a night of tuning: the option that helps a COBOL program is usually the one that changes the data, not the one that changes the C compiler. The COMP-3 lesson and the one on COMP and COMP-5 are where that starts.

-std=

-std picks the dialect. cobol85 is the 1985 standard, which has no functions, so the FUNCTION TRIM of the program is not a function of that standard:

cobc -x -free -std=cobol85 -o opt85 opt.cob

opt.cob:13: error: FUNCTION 'TRIM' unknown
11 | END-PERFORM.
12 | DISPLAY "total " WS-T.
13 > DISPLAY FUNCTION TRIM("abc").
14 | STOP RUN.
15 | <EOF>

The message is the one an unknown function always gets, and it is the whole argument for a dialect flag: a program that must run on a compiler of 1985 is checked here against the language of 1985, without the newer words quietly working and failing later.

The dialects this build knows are listed by cobc --help under -std=: default, cobol85, cobol2002, cobol2014, xopen, ibm, mvs, mf, bs2000, acu, rm and gcos, the last six with a -strict form each. The course builds everything with the default, which is the COBOL of this compiler with its extensions on.

-fsyntax-only

-fsyntax-only parses the program and stops before the C compiler and the link. On a good program it prints nothing and exits with 0, which is what a gate needs:

cobc -fsyntax-only -free opt.cob
echo $?

0

On a program with a misspelled verb it prints the error, the source line and a note, and exits with 1:

cobc -fsyntax-only -free bad.cob
echo $?

bad.cob:4: error: syntax error, unexpected Literal, expecting SECTION or .
2 | PROGRAM-ID. BAD.
3 | PROCEDURE DIVISION.
4 > DISPLY "x".
5 | <EOF>
bad.cob:4: note: unknown statement 'DISPLY'
1

The note is the line to read: the parser recovered at the literal and the note names the word it did not know. A pre-commit hook that runs -fsyntax-only on every COBOL file catches a misspelling in a second without building anything, and that is the check the course itself runs on its extracted programs.

Exercises

  1. Build the program with -g and compare the size of the executable with the plain one.
  2. Change the loop to add a COMP-5 field and measure the three runs again.
  3. Build with -std=cobol2002 and see whether the program of the page still compiles.
  4. Put the -fsyntax-only check of a bad file in a shell script that stops on a non-zero status.

Questions

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

  1. What does -x ask for?

    Answer

    An executable program

    The page built opt0 with -x and ran it.

  2. What does -c produce?

    Answer

    An object file for a link line

    The object was the 6912-byte file of the page.

  3. What did -O2 change about the loop of the program?

    Answer

    Nothing measurable: the runs of both builds overlap

    The three runs of each build covered the same range of seconds.

  4. Why does -std=cobol85 refuse FUNCTION TRIM?

    Answer

    The 1985 standard has no intrinsic functions

    The build printed FUNCTION TRIM unknown.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(3) VALUE 10. 01 W-B PIC 9(3) VALUE 20. 01 W-T PIC 9(4) VALUE 0. PROCEDURE DIVISION. ADD W-A TO W-T ADD W-B TO W-T DISPLAY W-T. STOP RUN.

    Answer

    0030

    Ten and twenty add to thirty in a four-digit field.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9(3) VALUE 500. PROCEDURE DIVISION. DISPLAY FUNCTION TRIM("ok"). DISPLAY W-N. STOP RUN.

    Answer

    ok then 500

    The function returns trimmed text and the field shows its three digits.

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

    Answer

    000010

    One and two and three and four add to ten.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(2) VALUE 99. 01 W-B PIC 9(2) VALUE 1. 01 W-S PIC 9(3). PROCEDURE DIVISION. ADD W-A W-B GIVING W-S DISPLAY W-S. STOP RUN.

    Answer

    100

    Ninety-nine and one add to a hundred, in a three-digit field.

  9. Which option is the gate that a commit hook runs on a COBOL file?

    Answer

    -fsyntax-only, which exits 1 on an error and prints nothing on a good file

    The page shows both statuses of that option.

  10. What is a fair conclusion from the -O2 measurement?

    Answer

    The cost of this loop is in the runtime, so the C compiler flag changes the size and not the time

    The executable was 28 per cent smaller and the three runs of each build overlapped.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64), 4 CPUs, in a Docker container.

Article author: Arthur Isaev