This repo includes several exercises illustrating simple COBOL programs.
The following instructions are optimized for a Mac user running VS Code.
When opening in VS Code for the first time, install the COBOL Source colouriser extension for easier viewing.
-
Clone this repo to your local machine.
-
cd
into the cloned repo folder. -
From the CLI, run
brew install open-cobol
.
The following program shows you how to print the string, "Ciao!" using COBOL.
-
The "Ciao!" text can be found in the
test.cbl
file.cd
into the folder where this file is located.To compile the
test.cbl
file into an executable binary, runcobc -free -x -o test test.cbl
.The
-free
flag tells GnuCOBOL to be more lenient when parsing code (rather than requiring extremely precise formatting), and the-x
flag tells GnuCOBOL to generate an executable file. -
Run
ls
. As you can see, you have a binary executable file calledtest
that you can execute. To do so, run./test
.Note: If you cloned this repo, the executable files should already be included. I am adding this step to illustrate how to create an executable in COBOL if you wish to create a new program.
-
You should now see the text,
Ciao!
printed in your CLI.
The following program shows you how to multiply two single-digit inputs using COBOL.
-
From the CLI, run
cobc -free -x multiply.cbl
. -
The code with the multiply logic is located in the file
multiply.cbl
. As in the previous exercise, compilemultiply.cbl
into an executabe binary file. To do so, runcobc -free -x multiply.cbl
. -
To run the executable you just created, run
./multiply
in the CLI. -
When prompted, enter any one-digit number. For this example,
3
. -
When prompted, enter a second one-digit number. For this example,
7
. -
You should see the following output:
Result is = 21
Sources:
Linux Magazine Tutorial - COBOL
WTF is COBOL?
GnuCOBOL Guides
GnuCOBOL Forum