Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Modern COBOL and Mainframes

GnuCOBOL and Modernizing COBOL

Set up GnuCOBOL on your own machine, compile with cobc, understand fixed and free source format, debug and test COBOL, and learn modernization strategies - APIs, refactoring, version control and migration.

You do not need a mainframe to practice COBOL. GnuCOBOL is a free, open-source compiler that translates COBOL to C and then to a native executable. It runs on Linux, macOS and Windows, supports most of COBOL-85 and large parts of COBOL 2002 and 2014, and many IBM and Micro Focus extensions. This lesson shows how to use it day to day, then widens the view to the real-world job of modernizing old COBOL systems: keeping them running, understandable and connected to newer technology.

Installing and compiling

Packages exist for most systems (apt install gnucobol, brew install gnucobol, Windows builds from the project's site and from MSYS2). Check the version with cobc --version; version 3.2 or later is recommended.

Terminal
cobc -x -o hello hello.cob        # compile to an executable (-x) named hello
./hello                           # run it
cobc -x -free -o hello hello.cbl  # free-format source
cobc -m taxcalc.cob               # build a callable module (taxcalc.so / .dll)
cobc -x main.cob taxcalc.cob      # compile and link two programs together

Useful options:

Option Meaning
-x Build an executable main program
-m Build a dynamically loadable module for CALL (CALL)
-free / -fixed Source format
-std=ibm, -std=mf, -std=cobol2014, -std=default Dialect: reserved words and extensions
-g Debug information
-debug Runtime checks: subscripts, reference modification, numeric data (tables)
-Wall Enable warnings
-I dir Copybook search path (copybooks)
-j Compile and run immediately

Environment variables control the runtime, for example COB_FILE_PATH (where files are found), COB_LIBRARY_PATH (where modules are found for dynamic CALL) and COB_SET_TRACE (tracing). Files named in ASSIGN TO are looked up relative to the current directory unless COB_FILE_PATH says otherwise.

Fixed and free format

Traditional COBOL source is fixed format, from punch-card days. Column positions have meaning:

Columns Use
1 to 6 Sequence numbers (ignored)
7 Indicator: * comment, - continuation, / page eject, D debugging line
8 to 11 Area A: division, section and paragraph names, FD, level 01 and 77
12 to 72 Area B: all other statements and data items
73 to 80 Identification (ignored, usually blank)

Text beyond column 72 is ignored, a classic source of bugs: a long statement is silently cut off. Free format (-free, standard in COBOL 2002) removes all of this: code may start anywhere, comments begin with *>, and lines can be longer. New code written for GnuCOBOL is often free format, while mainframe code stays fixed. Keep the same format within a project.

The next stand-in is a tiny linter for fixed format; it catches the usual mistakes: code in the sequence area, text past column 72 and a wrongly placed paragraph name.

Python Stand-in: a fixed-format column checker

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.

The checker flags line 7 (text past column 72), line 8 (text in the sequence area) and, for line 8, a misplaced paragraph name.

Debugging and testing

  • Compile with -debug while developing, so a bad subscript or a non-numeric field stops the run with a message and the line number, instead of corrupting data.
  • DISPLAY tracing is the traditional tool; GnuCOBOL's COB_SET_TRACE=Y prints each paragraph entered.
  • gdb works with -g; the GnuCOBOL IDE and VS Code extensions add stepping through the COBOL source.
  • On the mainframe, IBM Debug, Xpediter and Code Coverage play that role.

Tests deserve more attention than they usually get in COBOL shops. Practical approaches:

  1. Characterization tests: before changing legacy code, run it on real inputs and save the outputs as the baseline. After the change, compare byte for byte. This is the cheapest, most valuable test in batch systems.
  2. Unit tests for subprograms: write a small driver that calls the subprogram with known values, or use a framework such as COBOL Check, which generates test code from simple specifications (TESTSUITE, TESTCASE, EXPECT), or zUnit on IBM systems.
  3. Control totals inside programs (sequential files): record counts and hash totals that must reconcile.
  4. Continuous integration: a script that compiles everything with -Wall -debug, runs the tests and diffs the output.
Python Stand-in: a characterization test comparing baseline and new output

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.

The result is FAIL: output changed followed by the three-line difference, which is exactly the signal a regression test should give.

Differences from IBM COBOL

If you move code between GnuCOBOL and IBM Enterprise COBOL, expect these areas of difference:

  • LINE SEQUENTIAL files exist in GnuCOBOL (text files) but not on z/OS.
  • Collating sequence: ASCII versus EBCDIC (mainframe environment); sorting and comparisons of mixed text differ.
  • COMP sizes and byte order: GnuCOBOL's binary items are big-endian by default (-fbinary-byteorder), and COMP-5 is native. COMP-3 is the same.
  • File status values and VSAM features: GnuCOBOL's indexed files use its own storage (Berkeley DB, VBISAM, or built-in), not VSAM.
  • Embedded SQL and CICS: needs separate preprocessors (for example Ocesql for PostgreSQL, or a CICS emulator); they are not built in.

A shop that wants to run mainframe COBOL on Linux without rewriting typically buys a commercial platform (Micro Focus Enterprise Server, AWS Mainframe Modernization, Rocket) rather than hand-converting.

Modernization strategies

Billions of lines of COBOL still run production systems. "Modernize" can mean very different things; the common paths:

flowchart TD
    A["Legacy COBOL system"] --> B["Keep and maintain<br/>tests, docs, tooling"]
    A --> C["Wrap and extend<br/>APIs on top: CICS web services, z/OS Connect"]
    A --> D["Re-host<br/>move to Linux or cloud, same code"]
    A --> E["Refactor<br/>clean code, extract subprograms, remove dead code"]
    A --> F["Rewrite<br/>new platform, new language"]

The paths in order of rising cost and risk. Most large organizations combine several of them.

  1. Keep and maintain: invest in source control, automated builds and tests. Moving source from PDS libraries into Git, with a pipeline that compiles on every change, is often the first and best step.
  2. Wrap and extend: expose business functions as APIs (JSON and XML). New web front ends talk to the proven COBOL core.
  3. Re-host: run the same programs on another platform with a compatible runtime. Little code changes, so the risk is lower, but the code stays COBOL.
  4. Refactor: break up very long programs; replace GO TO with structured PERFORM; convert repeated logic into subprograms or copybooks; delete unreachable paragraphs found with coverage tools. Keep the characterization tests close.
  5. Rewrite: translate to Java or C# (automatically or by hand). It carries the highest risk: the business rules in decades of patches are hard to specify, and many rewrites have failed or been abandoned.

Good practice whichever way you go: find out what the program really does before touching it (read it, run it, test it), change one thing at a time, and keep an escape hatch.

Style that ages well

  • Use meaningful names (CUSTOMER-BALANCE, not WS-X1).
  • Prefer structured control flow: EVALUATE, inline PERFORM, END-IF scope terminators; avoid GO TO and ALTER.
  • Keep paragraphs small and give each one a comment saying what it does.
  • Put magic numbers in named constants (78 level items or 01 with VALUE).
  • Use 88 condition names for flags.
  • Avoid clever redefinitions; document every REDEFINES.
  • Compile with warnings on and treat them as errors in the pipeline.

Careers and skills

COBOL experience is valued because the people who know it are retiring while the systems remain. A COBOL developer today usually also knows JCL, VSAM, Db2 and CICS (this track), a source-control tool, and increasingly Java, REST and DevOps skills. Practicing on GnuCOBOL with the projects in the next chapter builds exactly that foundation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which cobc option builds an executable main program?

    -x creates an executable; -m builds a module for dynamic CALL; -free selects free format.
  2. 2. In fixed-format source, where do paragraph names start?

    Division, section and paragraph names, FD and 01 levels start in Area A. Other statements use Area B.
  3. 3. What does a characterization test do?

    Before refactoring you capture what the program does today, then check that it still does the same.
  4. 4. Which modernization path leaves the COBOL code unchanged but runs it on another platform?

    Re-hosting moves the system to a compatible runtime on Linux or the cloud with little code change.

Summary

  • GnuCOBOL (cobc -x) lets you compile and run COBOL on any desktop; use -debug and -Wall while learning.
  • Fixed format has meaningful columns (7 indicator, 8-11 Area A, 12-72 Area B); free format removes them.
  • Test legacy code with characterization tests, unit drivers and control totals before refactoring.
  • Modernization ranges from maintain, wrap and re-host to refactor and rewrite; most organizations mix them.

Next: Project: a payroll report