Install GnuCOBOL on Linux, Windows, macOS and FreeBSD

One compiler is enough for this course: GnuCOBOL, which translates COBOL to C and then uses the C compiler the system already has. That second part is why a COBOL install is never just one package, and why the first check after an install compiles something instead of printing a version.

This lesson installs the compiler on each family of systems, checks it with a program, and shows where cobc looks for the copybooks that later lessons include. The package names and the version below were read on the machine described at the end of the page; the routes for systems this container does not have are instructions, and each one has its own page on this site.

What a build needs

Two programs, and the second one is usually already there. The COBOL compiler reads the source and writes a C file; a C compiler turns that file into machine code; a linker puts the COBOL runtime library beside it. The runtime is libcob, and it holds the verbs a program calls: DISPLAY, the arithmetic, the file handling.

On most systems the COBOL package depends on the C compiler and on the development files of the runtime, so one install command brings all three. The Alpine package below says so in its own metadata: its dependencies include gcc, musl-dev and libcob4.

Linux packages

On Alpine the package is gnucobol, and apk reports what it would install. The three lines below are the whole description of the package on this machine.

apk add gnucobol
apk info -v gnucobol

gnucobol: GnuCOBOL compiler
gnucobol: https://www.gnu.org/software/gnucobol/
gnucobol: 2258 KiB

Debian, Ubuntu and their derivatives call the package the same way, and apt installs it with the C toolchain:

sudo apt update
sudo apt install gnucobol

On Fedora and on Rocky Linux the package comes from the distribution repositories, or from EPEL on a RHEL rebuild that does not carry it:

sudo dnf install gnucobol

Each of those routes has its own page with the version check and the PATH: Ubuntu, Rocky Linux. If you would rather not touch the system at all, COBOL in Docker puts the same compiler in a container with a fixed version, which is how the pages of this course were built.

Windows, macOS and FreeBSD

macOS uses Homebrew, and the package is again gnucobol:

brew install gnucobol

FreeBSD has it in the ports collection, so pkg installs a prebuilt binary:

sudo pkg install gnucobol

On Windows there is an installer on the GnuCOBOL releases page, and it adds the compiler to the PATH. The full steps, including the 64-bit choice and the PATH check, are on Install COBOL on Windows 11; the macOS page and the FreeBSD page do the same for their systems. None of those three was run here: this container is Linux only.

Check the install

cobc --version proves the binary is on the PATH. A program that compiles and runs proves the rest: the C compiler, the linker and the runtime library. The program below does real arithmetic in both of the ways COBOL keeps apart, truncation and rounding, so it fails loudly on a system where the numeric runtime is broken.

IDENTIFICATION DIVISION. PROGRAM-ID. INSTALL-CHECK. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-PI PIC 9V99999. 01 WS-THIRD PIC 9V9999. 01 WS-STAMP PIC X(21). PROCEDURE DIVISION. COMPUTE WS-PI = 22 / 7. COMPUTE WS-THIRD ROUNDED = 1 / 3. MOVE FUNCTION WHEN-COMPILED TO WS-STAMP. DISPLAY "pi: " WS-PI. DISPLAY "third: " WS-THIRD. DISPLAY "built: " WS-STAMP. DISPLAY "install check passed". STOP RUN.

cobc -x -free -Wall -o check check.cob
./check
echo "status: $?"

pi: 3.14285
third: 0.3333
built: 2026111805530845+0000
install check passed
status: 0

22 / 7 is 3.142857142857..., and a plain COMPUTE keeps the five decimals the picture declares and drops the rest: 3.14285, not a rounded 3.14286. The second line asks for ROUNDED and gets 0.3333. Both behaviours are the language, not a compiler accident, and the lesson on ROUNDED takes them apart. WHEN-COMPILED is the moment the compiler read the file, stored in a 21-character stamp.

Where cobc looks

cobc --info prints the configuration the binary was built with. The lines below are the ones a later lesson depends on: where copybooks are searched for, whether the machine is 64-bit, the byte order, and the libraries this build was compiled against. The BDB handler is the one that makes indexed files work, and the two library lines are what JSON and XML support are made of.

cobc --info | grep -E 'COB_(COPY|CONFIG)_DIR|64bit-mode|endianness|indexed file handler|JSON library|XML library'

COB_CONFIG_DIR : /usr/share/gnucobol/config
COB_COPY_DIR : /usr/share/gnucobol/copy
64bit-mode : yes
endianness : little-endian
indexed file handler : BDB
XML library : libxml2
JSON library : cjson

A copybook is a file of declarations that several programs share; the COPY statement pulls it in. This build looks in /usr/share/gnucobol/copy and in the current directory, and -I adds another folder. The lesson on COPY and copybooks uses that, and the lesson on cobc options lists the flag.

Exercises

  1. Install the compiler on your own machine with the route for your system, then run cobc --version and compare the first line with the one printed above.
  2. Change WS-PI to PIC 9V9999999 and predict the printed value of 22 / 7 before you run it.
  3. Run cobc --info on your machine and find the lines for the indexed file handler and the JSON library. Are they the same as here?

Questions

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

  1. Which two compilers take part in a GnuCOBOL build?

    Answer

    The COBOL compiler and a C compiler

    cobc writes C and the system C compiler turns it into machine code, so the C toolchain is part of the install.

  2. What does apk info -v gnucobol report on this machine?

    Answer

    The installed package, its homepage and its size

    The three lines name the installed package version, its webpage and how much space it takes.

  3. Which file does cobc --info name as the copybook folder?

    Answer

    COB_COPY_DIR

    COB_COPY_DIR is the directory the compiler searches for a COPY member, here /usr/share/gnucobol/copy.

  4. Why is a version check alone not enough to prove an install works?

    Answer

    Because the C compiler and the runtime library are only exercised by a build and a run

    A printed version says nothing about the C compiler, the linker or libcob, and a compiled program uses all three.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9V99. PROCEDURE DIVISION. COMPUTE A = 2 / 3. DISPLAY A. STOP RUN.

    Answer

    0.66

    Without ROUNDED the third decimal is dropped, not rounded up.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9V99. PROCEDURE DIVISION. COMPUTE A ROUNDED = 2 / 3. DISPLAY A. STOP RUN.

    Answer

    0.67

    ROUNDED keeps the value nearest to the true quotient at the declared scale, and 0.666... is nearer to 0.67.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 S PIC X(21). PROCEDURE DIVISION. MOVE FUNCTION WHEN-COMPILED TO S. DISPLAY FUNCTION LENGTH(S). STOP RUN.

    Answer

    21

    WHEN-COMPILED is a 21-character stamp, and LENGTH reports the length of the field it was moved into.

  8. What is the first line of the compiler output for this program? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9V99 VALUE 1.234. PROCEDURE DIVISION. DISPLAY A. STOP RUN.

    Answer

    warning: value size exceeds data size [-Wothers]

    The literal has more decimals than the picture and cobc warns about it; the value is truncated and the build succeeds.

  9. On Alpine, which package provides the cobc command?

    Answer

    gnucobol

    The gnucobol package provides cobc, cobcrun and cob-config; libcob4 is only the runtime library.

  10. Which command checks that a COBOL source file is syntactically valid without building it?

    Answer

    cobc -fsyntax-only file.cob

    -fsyntax-only parses and checks the program and writes no output file; -C writes the generated C instead.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container. The Windows, macOS and FreeBSD install steps were not run here.

Article author: Arthur Isaev