Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 38 ยท Getting Started

Hello, World with GnuCOBOL

Install GnuCOBOL, write your first COBOL program, compile it with cobc, run it, and read the most common compiler messages. Includes a mini DISPLAY simulator.

To learn COBOL you need a compiler. The best free choice is GnuCOBOL, an open-source compiler that translates COBOL to C and then to a native executable. It follows the standard closely and can imitate several vendor dialects, including IBM's. This lesson installs it, compiles a first program and explains what each step does.

This site cannot run COBOL in the browser, so the COBOL listings here are read-only; the two runnable blocks are small Python stand-ins that help you understand what the compiler does.

Installing GnuCOBOL

  • Linux: sudo apt install gnucobol (Debian, Ubuntu) or sudo dnf install gnucobol (Fedora). Check the version you get; 3.2 or newer is ideal.
  • macOS: brew install gnucobol.
  • Windows: use a prebuilt GnuCOBOL package, or install it inside WSL and follow the Linux steps.
  • Docker: any Linux image with the package installed works well if you do not want to change your machine.

Check the installation:

Terminal
cobc --version

cobc is the compiler driver. It prints the version of GnuCOBOL and the C compiler it uses.

Your first program

Save this as hello.cob. Notice the seven leading spaces on every line; the reason is in source format and coding rules.

COBOL
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.
       PROCEDURE DIVISION.
           DISPLAY "Hello, world!".
           STOP RUN.

Line by line:

  1. IDENTIFICATION DIVISION. starts the program and names it.
  2. PROGRAM-ID. HELLO. gives the program its name (HELLO).
  3. PROCEDURE DIVISION. starts the executable statements. This program has no data, so it has no DATA DIVISION.
  4. DISPLAY "Hello, world!". writes the text to the screen (standard output) followed by a newline.
  5. STOP RUN. ends the program and returns control to the operating system.

Every sentence ends with a period.

Compiling and running

Terminal
cobc -x hello.cob -o hello
./hello
TEXT
Hello, world!

-x means "build an executable". Without it cobc builds a loadable module, which is for subprograms (covered later in the course) called from other programs. -o names the output file. Other useful options:

Option Meaning
-free Treat the source as free format (no column rules).
-Wall Turn on all warnings.
-std=ibm Accept IBM-style extensions (also -std=cobol85, -std=default).
-debug Add runtime checks such as subscript range checks.
-g Add debugging information.
-I dir Search dir for copybooks.
-j Compile and run in one step (cobc -xj hello.cob).

Free format variant

If you prefer to write without the column rules, GnuCOBOL accepts free format:

COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
    DISPLAY "Hello, world!"
    STOP RUN.

Compile it with cobc -x -free hello.cob. Real mainframe code is fixed format, so this course mostly uses fixed format. Both examples are valid and do the same thing.

What the compiler does

From source to program

Write hello.cob
cobc COBOL to C
C compiler C to machine code
Run ./hello

GnuCOBOL translates through C. IBM compilers go straight to machine code.

1 / 2

A DISPLAY simulator (Python stand-in)

This runnable block is not COBOL. It is a tiny Python program that does what the compiler and runtime do for a program made only of DISPLAY statements: split it into sentences by periods, find the DISPLAY verbs and print their text.

Python Mini DISPLAY interpreter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading compiler messages

A missing period or a misspelt word gives a message such as:

TEXT
hello.cob:5: error: syntax error, unexpected "STOP RUN"

The file and line come first, then the kind of problem. Typical causes for beginners:

  • The line starts in the wrong column (text in column 7 or earlier).
  • A string literal is missing a closing quote.
  • A missing period ends the previous sentence too late, so the next verb is unexpected.
  • A word you chose as a name is a reserved word.

Fix the first error first; later messages often disappear with it.

Counting bytes: why columns matter (Python stand-in)

Fixed-format COBOL treats the first six columns as a sequence-number area. A program with line numbers in it is still valid:

Python Sequence area and code area

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command builds hello.cob into an executable named hello?

    The -x option builds an executable and -o names it. Without -x you get a module, not a program.
  2. 2. What does STOP RUN do?

    STOP RUN terminates the run unit. In a called subprogram you would use GOBACK instead.
  3. 3. A compile error appears on a line that looks correct. What is a very common cause?

    A missing period shifts where the compiler thinks a sentence ends, so the next verb looks out of place.
  4. 4. Which option tells cobc the source is free format?

    -free removes the fixed column rules. -x builds an executable, -j runs it and -I adds a copybook search directory.

Summary

  • GnuCOBOL's cobc compiles COBOL; -x builds an executable, -free accepts free format.
  • A minimal program has an IDENTIFICATION DIVISION, a PROGRAM-ID and a PROCEDURE DIVISION.
  • Every sentence ends with a period and STOP RUN ends the program.
  • Compiler messages name the file and line; fix the first error first.
  • The browser runner here uses Python stand-ins, not real COBOL.

Next: Source Format and Coding Rules