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!
Comments on this entry are closed.
very useful.thanks for sharing.but anyway I thought cobol is an obsolete programming language. 🙂
Nice!!!
Cobol is not very old programming language. I see on http://en.wikipedia.org/wiki/COBOL. I think is more useful to try python … Anyway is a nice article .
Ramesh as always great post,
Two quick question:
Does this compiler include Runtime? If not would you happen to know if there is any free Cobol Runtime licenses?
Thanks in advance
David
Compile fails with
“cannot find lncurses”
i am getting Problem while creating and using Vim,Cobol compiler.
Can any one help me out.
Important note on Debian / Ubuntu machines:
OpenCobol cannot find ncurse, so compile will fail. To fix this:
Edit ld.so:
sudo gedit /etc/ld.so.conf
and add ‘/usr/local/lib’
to the file.
eg:
include /etc/ld.so.conf.d/*.conf /usr/local/lib
Create symbolic link to ncurses:
sudo ln /lib/libncurses.so.5.7 /lib/libncurses.so
Re-run ld config:
sudo ldconfig
how to run the cobol program in z/OS.
program is to be currect i know but how it will run on cobol compiler plz tell me.
A lot of lecturers make it difficult for students to learn COBOL but it will be easy if you allow student to learn directly via the way you solved that very simple HELLO program. I bet i learn’ t a bit from that program you just wrote.
Just wandering what is the best COBOL Compiler for college students? how to compile COBOL program and can you please give me a sample program based on that version of COBOL. Tnx
The best compiler can be found in Pre historic caves package in adobe boxes.
how to install cobol program
I need some one to teach me a COBOL progame.
i want to learn more about cobol ….and how to compile a cobol program?????
nice to learn more about cobol..i love it…can you give some other example of program by using cobol.,???
why only linux or unix are recommanded , if we can use in other os like windows how can we use it?
My I have information on how to install a cobo compiler and editor, the files
cobc helloworld
helloworld:1: Error: Invalid indicator ‘F’ at column 7
This is on my Ubuntu 13.04 box with:
$ uname -a
Linux xeno-ThinkPad-T410 3.8.0-32-generic #47-Ubuntu SMP Tue Oct 1 22:35:23 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
I am using open cobol and I think I am following the instructions, so there must be another problem. Oh, the cksum of the file pasted from above is:
$ cksum hw.cob
521470958 141 hw.cob
xeno@xeno-ThinkPad-T410:~/shop/flatshop/codfish/top/examples/hello$ cksum helloworld
521470958 141 helloworld
Nice done
DISPLAY ‘Hello, world’
You see that “sentence” up there? Yup, that is actually an entire “hello world” program in COBOL…
It’s so easy, a seasoned programmer can learn it in only 3-4 weeks.
Oh, COBAL also runs about 70% of the worlds infrastructure… Yea, you heard that right. Everything from payroll to gas pumps & ATM machines to life support systems in the hospital. Pretty much anything mission critical that simply CAN NOT CRASH runs on COBAL to this very day.
More lines of code are in use for COBOL than for all other programming languages COMBINED. The average American comes in contact with it 13 times a day on average!
COBOL truly is the 10 ton elephant in the middle of the room. It’s ANCIENT but it works. It has been in place SO LONG that it cannot be replaced. Believe me, they have tried, over and over and over again and it just costs too much and whatever language they try to replace it with is just is not as reliable.
And lastly, the programmers for COBOL are rapidly aging and retiring or already long gone… but COBOL isn’t going ANYWHERE. Not in YOUR lifetime.
All that being said, you may want to take a closer look into it. COBOL is a seriously BORING language used for BORING stuff. But big companies NEED COBOL programmers like junkies need junk. They will PAY you TOP DOLLAR.
And there’s no competition. Period. The trick is, knowing where to look…
i found cobol very inrtresting with help of my lecturere and this acticle
In today’s time, if you want to stand out as a programmer then Cobol is the buzzword. I saw these old grey haired Cobol programmers who are earning 3 to 5 times higher then conventional Java or DotNet programmers…
Micro Focus has also developed Visual Cobol IDE that works on top of Visual Studio or Eclipse IDE for RAD and Agile development…
All of the world’s largest Banks and Insurance companies are running Cobol…
Total number of searches on Google in a day are lesser than total number of transactions done in a Cobol system.
The reason, right from the horse’s mouth – It’s so stable, it’s peace of mind at the end of the day!
Hi ! , to awake this nice page I want to give my humble opinion!
Cobol isn’t ready to die ! Like latin or greek, many people use dead languages and Cobol is very litteral and powerfull.
On big systems but mot only, it’ s the stablest compared to many others. If somebody wants a short example more structured that “hello World”, I will write a bill more here !
I’m very interest by this present article and thank the author RAMESH for having done it. There are old things which persist, Cobol is a strong survivor gracefull to IBM, yeah !!!
It’s nice to speak about Linux and Open-Cobol , I like it, surely others too !
Hi ! Dears, I have seen light in this site and entered again !
Without a lot of comments let me share the following with you…
As I promised here you can find a small Cobol program compiled with open-cobol in Linux Kubuntu with this command : cobc -Wall -x moreorless.cob.
Then you find in your directory the program named moreorless. To start it just type ./moreorless in a konsole and enjoy !
(I typed the code exactly like here down and saved it with the name moreorless.cob , the line numbers are to help positionning correctly the lines of code I find it cool !)
Listing, here :
000010 IDENTIFICATION DIVISION.
000020 PROGRAM-ID. MoreOrLess.
000030*REMARKS.
000040* PLAYING MORE OR LESS.
000050 DATA DIVISION.
000060 WORKING-STORAGE SECTION.
000070
000080 77 seed PIC 9(8) VALUE 0.
000090 77 nbRandomized PIC 999.
000100 77 nbEntries PIC 999.
000110 77 nbEnter PIC 999.
000120 SCREEN SECTION.
000130
000140 1 pla-title.
000150 2 BLANK SCREEN.
000160 2 LINE 2 COL 15 VALUE ‘playing More Or Less !’.
000170
000180 1 pla-more.
000190 2 LINE 4 COL 5 VALUE ‘It is more !’.
000200
000210 1 pla-less.
000220 2 LINE 4 COL 5 VALUE ‘It is less’.
000230
000240 1 pla-found.
000250 2 LINE 4 COL 5 VALUE ‘Hurra ! you got it !’.
000260 2 LINE 8 COL 5 VALUE ‘Game OVER.’.
000270 1 pls-nb.
000280 2 LINE 6 COL 5 VALUE ‘Please enter a number : ‘.
000290 2 PIC zzz TO nbEntries REQUIRED.
000300
000301 1 pls-ent.
000302 2 LINE 10 COL 5 VALUE ‘Enter 0 to finish : ‘.
000303 2 PIC zzz to nbEnter REQUIRED.
000310 PROCEDURE DIVISION.
000320
000330 INITIALIZE nbEntries.
000340 MOVE FUNCTION CURRENT-DATE(9:8) TO seed.
000350
000360 DISPLAY pla-title.
000370
000380 COMPUTE nbRandomized = FUNCTION RANDOM (seed) * 100 + 1.
000390
000400 PERFORM UNTIL nbEntries = nbRandomized
000410 DISPLAY pls-nb
000420 ACCEPT pls-nb
000430
000440 IF nbEntries > nbRandomized THEN
000450 DISPLAY pla-less
000460 ELSE
000470 DISPLAY pla-more
000480 END-IF
000490 END-PERFORM.
000500
000510 DISPLAY pla-found.
000520 DISPLAY pls-ent
000521 ACCEPT pls-ent
000530 STOP RUN.
So, copy and past it in a small text-editor like notepad, wordpad, kwrite or gwrite or nano, vim and so on… and save it as is with the “name you want.cob” or “name you want.cbl”. Compile it and go with ./name (without extension)! I expect that you better this peace of code e.g. with a counter.
(You will remark the formatting mode in the listing so that the position of procedure, label, command, remarks are respected).
the rules to follow are like here :
Standard ANS’85 Cobol programs are laid out as follows:
Columns 1-6 Sequence number, may be left blank
Column 7 Indicator column, may have a value of:
* – line is a comment
– – line is a continuation
D – line is a ‘debugging line’
Columns 8-11 Area A
Columns 12-72 Area B
Certain things need to, or can, start in Area A such as labels, and
other things, such as statements, need to start in Area B.
Some Cobol systems have extensions that allow free format source,
OpenCobol does not.
Well, I hope you will find your luck in this small “aparte”, thanks for your attention.
it is very useful 4 me
thank u all….!
Hi Team,
I tried to execute a program in GNU cobol to check the precision of decimal point in a COMP-2 variable e.g 5/3. The result gets rounded up at the last digit.
The result comes out to be 1.66666666666666667.
Is there any compiler option or any setting in the configuration file to overcome this.
The same program I executed in ZOS mainframe and the result comes up to be .16666666666666665E 01
Just an FYI per the “Hello World” example, I tried it today (yes, several years after you published) and got an error on compilation step. Basically it was rejecting the * asterisk as the comment marker.
Per https://stackoverflow.com/questions/25424250/cant-comment-and-other-problems-with-cobol
if you use *> and then your comment text, it will be fine.