Elemental SystemsSilicon · Boards · Software

Set Up Your Language · 14 of 14

Setting Up COBOL (GnuCOBOL)

COBOL was created in 1959 for business data, and it still runs a large part of the world's banking, insurance and government systems, mostly on big mainframe computers. It reads almost like English sentences. GnuCOBOL is a free COBOL compiler from the GNU project; on Windows we get it through MSYS2, the same free tool collection used on the C and C++ pages.

Set-up: Medium (more steps)Official source: gnucobol.sourceforge.ioFile ending: .cob

New to all this? Read Before you start on the main page first: it shows how to open Terminal, make a folder for your code, and see file name endings. It takes five minutes and prevents most problems.

1 What to download, and from where

Download only from gnucobol.sourceforge.io, the official GnuCOBOL project site; the Windows build comes from MSYS2 (msys2.org). Other download sites can bundle unwanted software.

  1. GnuCOBOL's home is gnucobol.sourceforge.io. The project publishes source code; for Windows we use the ready-built version from MSYS2.
  2. Go to msys2.org, the official MSYS2 site. Under Installation, click the installer link (a file named like msys2-x86_64-20xxxxxx.exe). (If you already set up C or C++ from this site, you have MSYS2: open MSYS2 UCRT64 and go to the pacman step.)

2 Install it on Windows

  1. Open the downloaded file. Click Next on each screen and keep the suggested folder C:\msys64. Click Finish at the end.
  2. A black window titled UCRT64 (or MSYS2 UCRT64) opens. This is MSYS2's own terminal. If it does not open, press the Windows key, type MSYS2 UCRT64 and open it.
  3. In that window, type this line and press Enter (copy it exactly):
    pacman -S --needed mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-gnucobol
    It lists what it will download and asks Proceed with installation? [Y/n]. Press Enter to say yes. It downloads for a minute or two.
  4. GnuCOBOL turns COBOL into C and then uses the C compiler, which is why that line installs both.

3 Check that it worked

In the MSYS2 UCRT64 window, type this and press Enter:

cobc --version

What you will see: A first line with cobc (GnuCOBOL) and a version number. Type this in the MSYS2 UCRT64 window.

cobc (GnuCOBOL) 3.2.0
Copyright (C) 2023 Free Software Foundation, Inc.

If you see an error such as "is not recognized" instead, look at the common mistakes at the bottom of this page.

4 Write and save your first program

A program is just a text file. You can write it in Notepad, which comes with Windows. (Later you may like the free Visual Studio Code editor from code.visualstudio.com, which colours the code and helps spot mistakes.)

  1. Open Notepad (press the Windows key, type Notepad, press Enter).
  2. Type the program below, or click Copy and paste it in with Ctrl + V. Every symbol matters, so copy it exactly.
           IDENTIFICATION DIVISION.
           PROGRAM-ID. HELLO.
           PROCEDURE DIVISION.
               DISPLAY "Hello, World!".
               STOP RUN.
  3. In Notepad choose File → Save as. Go to your C:\code folder. Set Save as type to All files (*.*), type the file name hello.cob exactly as shown, including the dot and the ending, and click Save.

COBOL programs are split into divisions. PROGRAM-ID names the program, DISPLAY prints the text and STOP RUN ends it. Every sentence ends with a full stop. The spaces at the start of each line matter: traditional COBOL code starts in column 8, so the first lines begin with 7 spaces and the last two with 11. Copying with the Copy button keeps them.

Do not use Word. Word saves extra formatting and changes straight quotes " into curly ones “ ”, which no programming language accepts.

5 Run it and see the result

Use the MSYS2 UCRT64 window, which already knows where GnuCOBOL and the C compiler are. Move into your folder (Windows C:\code is written /c/code there), compile, then run:

Type these lines one at a time, pressing Enter after each:

cd /c/code
cobc -x hello.cob
./hello.exe

What you will see: the words below on a new line. That is your program running.

Hello, World!

-x means "make a program I can run" (an .exe).

Congratulations: you have written and run a COBOL program.

6 Common beginner mistakes and how to fix them

Error messages look alarming but are usually precise: read the first line, and look for a line number. If you are stuck, copy the whole error message and paste it into an AI assistant along with your code, and ask what it means.

hello.cob:1: error: invalid indicator, or syntax error, unexpected Identifier

Why: the leading spaces were lost, so the code does not start in column 8 as traditional ("fixed format") COBOL expects.
Fix: put back the 7 spaces at the start of the lines (11 before DISPLAY and STOP RUN), or tell GnuCOBOL you are using free format: cobc -x -free hello.cob.

There is a hello.dll but no hello.exe

Why: you left out -x, so GnuCOBOL made a module for other programs to use instead of a program.
Fix: compile again with cobc -x hello.cob (or run the module with cobcrun hello).

bash: cobc: command not found

Why: you are in a different MSYS2 window (such as MSYS or MINGW64), or the install did not finish.
Fix: open the window named MSYS2 UCRT64 and run the pacman line again; it skips anything already installed.