GnuCOBOL (formerly known as OpenCOBOL and briefly as GNU Cobol) is a free implementation of the COBOL programming language that is part of the GNU project. GnuCOBOL translates COBOL code into C and then compiles it using the native C compiler.
sudo apt update && sudo apt upgrade
sudo apt install gnucobol
cobc --version
This will display the version of GnuCOBOL installed.
On Windows, the installation of GnuCOBOL can be done manually or via WSL2. Manual procedure:
Download GnuCOBOL: from the address: Direct to files on SourceForge
Extract the ZIP: to a directory of your choice, for example, C:\gnucobol
.
bin
directory to your PATH
:Right-click on “This PC” or “My Computer” and select “Properties”.
Click on “Advanced system settings”.
In the “Advanced” tab, click on “Environment variables”.
In the “System variables” section, find the Path
variable and click on “Edit”.
Add the path to the GnuCOBOL bin
directory (e.g. C:\gnucobol\bin
) to the end of the path list.
Click “OK” to save the changes.
cobc --version
After the installation, you can test GnuCOBOL by creating a simple COBOL program. For example, create a file called hello.cbl
with the following content:
NOTE: In Cobol it is important to respect these leading spaces!
*> Meu Hello World em GnuCobol
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
DISPLAY "Hello, World!".
STOP RUN.
To compile and run on Ubuntu or Windows, use the following commands:
cobc -x hello.cbl
./hello
If you see any warning it may be because
_FORTIFY_SOURCE
is being defined somewhere in your build environment. If this is bothering you, ignore it like this:cobc -x hello.cbl 2>/dev/null
.
On Windows, the command to run the program would be:
hello.exe
This should display “Hello, World!” in the terminal.
For macOS and BSD you can either use the system’s package manager or download it, just like on Windows, directly from SourceForge:
https://sourceforge.net/projects/gnucobol/files/gnucobol/3.2/
For more information, see this PDF, the Guide and the official address.