Michael HönnigMichael Hönnig

Listen to an audio recording of this blog post:

COBOL (COmmon Business Oriented [programming] Language) dates back to 1959, and is still used in its realm of business software, mostly on mainframe computers. Of course, there are also many related software-modernization and -migration projects. Thus, once in a while even non-mainframe developers might need some COBOL knowledge. Commercial COBOL systems, especially on real mainframes, are expensive. In this article I demonstrate how to set up the GnuCOBOL compiler and integrate it with Visual Studio Code on Ubuntu Linux, but it also works similar on other Linux distributions, Windows and macOS. This way, experimenting with COBOL becomes easy and cheap.

If you are not interested in some prosa about COBOL, but you just want to install and run that thing? Then jump to Installing the Compiler right away!

The Ideas Behind COBOL

From today’s perspective, COBOL is more like a DSL (domain specific language) in its realm of business applications for finance, accounting, business administration etc. Its core idea was enabling people from these subject areas to be able to read the business logic without deeper programming knowledge, e.g. for validation or audits. Many employees from business fields even switched their profession and became COBOL developers. If that all really worked out in the end, is another question.

Further, COBOL was made to deal with large amounts of data - at least what once counted as such. It directly supports, batch processing, reporting and online transaction processing (OLTP), the latter just through text-based user interfaces, though. With COBOL you can process random access files with multiple indices, create dialog screens and simple reports out-of-the-box without the need of any extra libraries.

One of its strength are monetary calculations through its intrinsic support of fixed point arithmetic and currency support. The latest standards even support object oriented programming, with a quite clumsy notation, though.

As a little anecdote: Did you know that the COBOL evangalist and one of the inventors of OO COBOL, Jerome Garfunkel (✝2018), was the brother of the famous musician Art Garfunkel?

Reasons for COBOL in 2020

According to a survey in the context of the COBOL60 celebrations (at about 45:05, needs registration) 87% of COBOL users expected that core business applications written in COBOL will still be in use when they retire. Most of these applications are running on mainframe computers.

But there are even reasons why software developers from outside of the mainframe world might need some knowledge in this legacy programming language, e.g. in the context of:

  • Understanding business logic coded in COBOL for software migration purposes.

  • Integrating COBOL software systems with systems using modern programming languages.

  • Exporting data from software written in COBOL, because sometimes that’s the easiest way to access the original data.

  • By my experience, old ideas often come back in new shapes. So knowledge about the these old ideas can help to come up with new ones.

I am sure there are many other reasons why one might need some COBOL knowledge these days, even without being a mainframe developer. Micro Focus, one of the major vendors or COBOL compilers and development environments, even made it possible to migrate COBOL software from mainframes to microservices running in the cloud! This opens the door for completely new levels of software integration.

I have only very limited COBOL experience, and the few projects where I got in touch with it, are decades ago. Just recently I needed to read some COBOL code again, which was actually the igniting spark for this blog post. Dealing with COBOL is fine for me, as long as it’s only a minor part of the project. Especially in the process of a software modernization, it can even be fun. But I’m certainly not interested, neither qualified in mainly COBOL projects.

Options for using COBOL with a PC

Without much research I ran across these options to use COBOL with a PC system:

  • The commercial product Micro Focus Visual COBOL, a 30-day free trial is available.

  • An online editor and compiler by Tutorialspoint based on GnuCobol 2.2, which currently seems to be down, though.

  • The MVS 3.8j Tur(n)key 4- System, a ready to use OS/VS2 MVS 3.8j system built for the Hercules IBM System/370, ESA/390, and z/Architecture mainframe emulator, which supposedly contains a COBOL compiler.

  • GnuCobol (formerly OpenCOBOL) , implements most parts of COBOL 85 to COBOL 2014 and other standards, as well as many extensions from COBOL vendors like IBM or Micro Focus.

In this article I’ll show how to install and use GnuCOBOL 3.1 together with Visual Studio Code (vscode) on an Ubuntu 20.04 system. There are also blog articles for Windows and macOS, both also using GnuCOBOL 3.1 as well.

Installing the Compiler

Ubuntu 20.04 only contains a package of GnuCobol 2.2, which works but is quite old. The recently released Ubuntu 20.10 contains even a nightly build of upcoming GnuCOBOL 4.0.

Because I wanted to stick with Ubuntu 20.04 for now, and prefer not to use bleeding edge testing versions, I decided to install GnuCobol 3.1-rc1. It’s not yet released either, but at least a release candidate. Here the installation steps ($ just indicates the command prompt, do no type it):

installing gnucobol-3.1-rc1 on Ubuntu 20.04
$ GNU_COBOL_DIR=$HOME/Programs/GnuCobol
$ wget https://sourceforge.net/projects/gnucobol/files/gnucobol/3.1/gnucobol-3.1-rc1.tar.gz/download
$ sudo apt-get install libdb-dev        # only for Berkeley DB, if needed
$ ./configure --prefix=$GNU_COBOL_DIR   # alternatively, add `--without-db`
$ make
$ make check
$ make install
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GNU_COBOL_DIR/lib
$ export PATH=$PATH:$GNU_COBOL_DIR/bin

Find further information about the installation process in the GnuCobol FAQ.

To try it out, run this in your shell ($ just indicates the command prompt, do no type it):

$ echo >hello-world.cbl <<EOF
       IDENTIFICATION DIVISION.
       PROGRAM-ID. hello-world.

       PROCEDURE DIVISION.
           DISPLAY "Hello world!".
           STOP RUN.
EOF
$ cobc hello-world.cbl
$ cobcrun hello-world

You just compiled and ran your first COBOL program!

What’s important here, the program-id hello-world has to be exactly match the module name which is the basename of the file. Otherwise, it won’t work and cobcrun will complain about not finding the module.

GnuCobol transpiles COBOL-code to C-code and then compiles the C-code to a shared-object-file (.so). It can also create executable files, but using cobcrun takes care for shared libraries and is easier to use for experiments.

Choosing an IDE

For my daily work, I prefer IntelliJ IDEA as an IDE: Unfortunately, as of now, there is no plugin available for COBOL, even though I found two experimental projects for such a plugin:

Then there is OpenCobolIDE, due to library version conflicts I was not able to get it to run on Ubuntu 20.04. Additionally, it’s not maintained anymore and even its supposed successor HackEdit looks quite dead for years.

There seem to be some plugins for the Eclipse IDE, which I have not tried, though. And the COBOL-plugins for NetBeans look all very outdated.

Finally, there is, Microsoft Visual Studio Code, a free source-code editor for Windows, Linux and macOS which also includes many IDE features. Here we get plenty of recently maintained COBOL plugins, surprise! I installed bitlang.cobol which comes with IntelliSense, highlighting, snippets and more. Feel free to try any of the others.

Integrating the Compiler into the IDE

It was bit awkward to always run the compiler and runtime after each change I’ve made to the source code during my experiments. For sure, with some more knowledge about Visual Studio Code, it would be possible to implement a real integration. But as this is not my usual IDE, and I needed it only for some experiments, I wrote a simple bash script:

watch-compile-and-run
#!/bin/bash

# --- configuration ---
gnu_cobol_dir=$HOME/Programs/GnuCobol
#gnu_cobol_cobc_args='--std=ibm-strict' (1)

# --- initialization ---
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$gnu_cobol_dir/lib:.
export PATH=$PATH:$gnu_cobol_dir/bin

cobc --version
echo "----"
echo

# --- wait for changes in file system ---
inotifywait -e close_write,moved_to,create -m . | (2)
while read -r directory events source; do
    ext="${source##*.}"
    if [ "$ext" = "cbl" ]; then (3)
        source_basename=$(basename -- $source .cbl)

        # ---- compile ----
        rm -f $source_basename.so
        compile_command_line="cobc $gnu_cobol_cobc_args $source"
        echo "---- compiling: $compile_command_line"
        $compile_command_line
        if [ $? -eq 0 ]; then

            # ---- execute ----
            execute_command_line="cobcrun $source_basename"
            echo "---- executing: $execute_command_line"
            $execute_command_line </dev/tty (4)
            if [ $? -eq 0 ]; then
                echo "---- DONE."
            else
                echo "---- FAILED."
            fi
        fi

        # ---- cleanup ---
        rm -f $source_basename.so

        echo
    fi
done
1 Here you can easily set compiler options for cobc, e.g. the COBOL-standard to use.
2 See https://superuser.com/questions/181517/how-to-EXECUTE_COMMAND_LINE-a-command-whenever-a-file-changes
3 Typical COBOL-file-name extensions for COBOL are .cob and .cbl, both lower- or uppercase. This script only recognizes lowercase `.cbl'.
4 The pipe from inotifywait redirects stdin, but our program might need the normal terminal (TTY) for input.

As COBOL has a maximum line length of 80 characters, I moved the vscode terminal window to the right and started my script: ./watch-compile-and-run. After displaying the compiler version, it waits for *.cbl files to change and then compiles and runs them automatically. Of course, this only works for single-module programs. Once your programs become larger, you’ll need a more professional setup.

Old Homepage
Figure 1. watch-compile-and-run in Visual Studio Code

Conclusion

It’s quite easy to get a pretty powerful COBOL development system to run on Linux. In general, it’s also possible to do this on Windows and macOS, they are just not my preferred development systems, so sorry, I don’t have instructions for these. You can find the instructions in the two external blog articles mentioned before.

But be warned, GnuCobol is not meant to be used for production systems. And if your code runs with GnuCobol, it does not mean that it also runs on commercial Cobol systems like Micro Focus COBOL or IBM COBOL or the other way around.

Any comments about this article are welcome on on Twitter.