How to compile and run a COBOL program?

44,356

Solution 1

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

Solution 2

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 

Solution 3

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...

Share:
44,356

Related videos on Youtube

Ajit MEHTA
Author by

Ajit MEHTA

Updated on September 18, 2022

Comments

  • Ajit MEHTA
    Ajit MEHTA almost 2 years

    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.

  • Bobble
    Bobble about 11 years
    Seeing this made a grey-haired geek happy.
  • Volker Siegel
    Volker Siegel over 9 years
    This 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.
  • sorrell
    sorrell almost 9 years
    @VolkerSiegel raises a good point. I had to change the comment to *> to get this to compile.