Can anybody explain me how to compile and run a COBOL program in Ubuntu? I have never written any program in Ubuntu. Please, give me a simple program to compile and run.
5 Answers
COBOL is not particularly popular on Linux but there are compilers available. One of these is open-cobol.
First step is to check if it's installed on your system: it probably isn't.
whereis cobc; which cobc
cobc:
If like my system it is not installed you can install it with
sudo apt-get install open-cobol
And to check its installed whereis cobc; which cobc
cobc: /usr/bin/cobc /usr/bin/X11/cobc /usr/share/man/man1/cobc.1.gz
/usr/bin/cobc
Now lets write our first program with any text editor.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
*> simple hello world program
PROCEDURE DIVISION.
DISPLAY 'Hello world!'.
STOP RUN.
save this as "helloworld.cbl"
We can now compile it with cobc -free -x -o helloworld helloworld.cbl
On my system I see this
$ cobc -free -x -o helloworld helloworld.cbl
/tmp/cob3837_0.c: In function ‘HELLO_2DWORLD_’:
/tmp/cob3837_0.c:75:7: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
/tmp/cob3837_0.c:76:7: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
/tmp/cob3837_0.c:77:7: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
/tmp/cob3837_0.c:88:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
/tmp/cob3837_0.c:107:5: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
/tmp/cob3837_0.c:111:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
A few warnings -- but no errors test with ./helloworld
Hello World!
It works.
Alternative (fixed format):
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
* simple hello world program
PROCEDURE DIVISION.
DISPLAY 'Hello world!'.
STOP RUN.
save this as "helloworld.cob" and compile it with cobc helloworld.cob
(run with cobcrun helloworld
.
If you want to remove the warnings from the C compiler: download a current GnuCOBOL 2.x snapshot (which has no updated package yet) and build it yourself (needs an additional apt-get bison flex libdb-dev curses-dev
).
Taken from:
Cobol Hello World Example: How To Write, Compile and Execute Cobol Program on Linux OS on thegeekstuff.com
Tested on Ubuntu 12.04.2
-
4
-
1This answer is good -- except that the comment character combination is actually
*>
or a single*
in column 7. The new user @David wrote this as an answer (he could not comment) - this is copying the content to a comment instead, to preserve it if the answer gets deleted. Mar 19, 2015 at 15:55 -
1@VolkerSiegel raises a good point. I had to change the comment to
*>
to get this to compile.– sorrellSep 1, 2015 at 1:17
You can use the open-cobol compiler. Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below:
sudo apt-get install open-cobol
cobc your_program_here.cbl
Ubuntu 22.04 and later
open-cobol is not available anymore in Ubuntu 22.04 and later. Install cobol with sudo apt install gnucobol4
This works.
There is good support for COBOL using Vim as an editor.
-
open-cobol
is still available for focal or 20.04; (open-cobol | 2.2-5 | focal/universe | all
) , but you are correct in thatgnucobol4
(eg.gnucobol4 | 4.0~early~20200606-6 | jammy/universe | source, amd64, arm64, armhf, ppc64el, riscv64, s390x
) is available for newer supported releases of Ubuntu.– guivercMar 21 at 11:48
Warren Hill gave a good answer. You can also use an IDE such as Eclipse to help with COBOL but I'm not sure that's appropriate if you have never programmed.
See the Eclipse COBOL forum, Eclipse Forums
I noticed one of the posts lists available COBOL plug-ins...
If you want an IDE I highly suggest to use the OpenCobolIDE (works with newer GnuCOBOL compilers as well). You find the latest package at https://launchpad.net/cobcide/+download