Question: I would like to understand the basics of how to write and execute Cobol program on Linux OS. Can you explain it with a simple example?
Answer: In this article, let us review very quickly how to write a basic Hello World Cobol program and execute cobol program on Linux or Unix OS.
1. Write a Hello World Cobol Program
Create the helloworld program using the Vim editor as shown below.
$ vim helloworld IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. * simple hello world program PROCEDURE DIVISION. DISPLAY 'Hello world!'. STOP RUN.
Note: Comment in Cobol starts with *.
2. Make sure Cobol Compiler is installed on your system
Make sure Cobol compiler is installed on your system as shown below.
$ whereis cobc cobc: /usr/bin/cobc /usr/share/man/man1/cobc.1.gz $ which cobc /usr/bin/cobc
Installing cobc compiler
If you don’t have cobol compiler, install it as shown below.
$ sudo apt-get install open-cobol
3. Compile the cobol program
Compile the HelloWorld which will create the HelloWorld executable.
$ cobc -free -x -o helloworld-exe helloworld $ ls helloworld helloworld-exe*
- -free – use free source format. Without this option cobol program requires certain format.
- -x – build executable program.
- -o FILE – place the output file into the specified FILE.
4. Execute the cobol Program
Execute by mentioning the program name.
$./helloworld-exe Hello World!