COBOL Tutorial

Progress0 of 0 lessons

COBOL Environment Setup

To write and run COBOL programs you need a compiler and a way to edit source code. This page walks you through setting up a COBOL development environment on your PC using GnuCOBOL (a free, open-source compiler), plus optional IDEs and editors. If you are targeting a mainframe, you will still need mainframe access for deployment; PC setup is for learning and local testing.

What you need

  • COBOL compiler – e.g. GnuCOBOL (cobc) to turn .cob/.cbl source into executables.
  • Text editor or IDE – Any editor (Notepad++, VS Code, etc.) or an IDE like OpenCobolIDE.
  • Terminal or command prompt – To run cobc and the compiled program.

Installing GnuCOBOL

GnuCOBOL compiles COBOL to C and then uses a C compiler (e.g. gcc) to build executables. Install the C compiler first if your OS doesn’t include it.

Windows

Download a GnuCOBOL Windows build (e.g. from the GnuCOBOL site or community builds). Many distributions bundle MinGW so you get both COBOL and C. Unzip to a folder (e.g. C:\GnuCOBOL) and add the bin directory to your PATH so you can run cobc from any terminal.

Linux

Use your package manager. For example: sudo apt install open-cobol or gnucobol on Debian/Ubuntu; sudo dnf install gnucobol on Fedora. This installs the cobc command and dependencies.

macOS

With Homebrew: brew install gnucobol. This installs cobc and the needed C compiler.

Verifying the installation

Open a terminal and run:

text
1
cobc --version

You should see the GnuCOBOL version. If the command is not found, check that the compiler’s bin directory is on your PATH.

Compiling and running a program

Create a file named hello.cob with this content:

cobol
1
2
3
4
5
6
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. DISPLAY 'Hello, COBOL!'. STOP RUN.

In fixed format, the code starts in column 8 (column 7 is for continuation or *). Save the file, then in the same directory run:

text
1
cobc -x hello.cob

-x means “produce an executable.” This creates hello (or hello.exe on Windows). Run it:

text
1
2
./hello REM On Windows: hello.exe

You should see Hello, COBOL! printed.

Common cobc options

Common GnuCOBOL (cobc) options
OptionMeaning
-xBuild an executable program
-mBuild a callable module (for CALL)
-freeUse free-format source (no column rules)
-o nameSet output file name
--helpShow compiler options
--versionShow GnuCOBOL version

Example: build an executable with a custom name and free-format source: cobc -x -o myprog -free myprog.cob.

Free-format source (-free)

Standard COBOL uses fixed format (area A and B, columns 7–72). GnuCOBOL supports free format with -free: you can put code in any column and use // or * for comments. Helpful when coming from other languages.

cobol
1
2
3
4
5
6
*> Free-format example (compile with: cobc -x -free prog.cob) identification division. program-id. free-demo. procedure division. display "Free format COBOL" stop run.

Editors and IDEs

  • VS Code – Install a COBOL extension for syntax highlighting and optionally lint/compile. You can run cobc from the integrated terminal.
  • OpenCobolIDE – GUI IDE for Windows, Linux, and macOS that integrates GnuCOBOL and helps compile and run programs.
  • Any text editor – Notepad++, Sublime, vim, etc. Save as .cob or .cbl and compile from the command line.

Mainframe vs PC development

GnuCOBOL on a PC is great for learning COBOL syntax, logic, and file handling. Mainframe COBOL (e.g. IBM Enterprise COBOL) uses a different dialect and runtime (JCL, datasets, etc.). To deploy on a mainframe you need mainframe access and the right compiler. Use PC setup for practice and prototyping; use the mainframe environment for production and mainframe-specific features.

Step-by-step: First program

  • Install GnuCOBOL (and a C compiler if needed) for your OS.
  • Add the compiler bin directory to PATH; run cobc --version to confirm.
  • Create a .cob file with IDENTIFICATION DIVISION, PROGRAM-ID, PROCEDURE DIVISION, and a DISPLAY.
  • Run cobc -x program.cob to build the executable.
  • Run the executable from the terminal.

Explain like I'm five

The compiler is like a translator: it reads your COBOL “recipe” and turns it into a program the computer can run. Installing GnuCOBOL gives you that translator. The editor is where you write the recipe. Once the recipe is saved, you tell the translator to turn it into a runnable program, then you run that program like any other app.

Test Your Knowledge

1. Which command creates an executable from a COBOL source file with GnuCOBOL?

  • cobc -c hello.cob
  • cobc -x hello.cob
  • cobc -o hello.cob
  • run hello.cob

2. What does the -free option do in GnuCOBOL?

  • Makes the compiler free to use
  • Enables free-format source code (column 7–72 not required)
  • Frees memory after run
  • Disables fixed format

Related Concepts

Related Pages