I’ve been thinking about learning Cobol programming for awhile. I figured I would pull out my rusty old book, “REA’s Problem Solvers Computer Science”, since it is the only reference book that I have to Cobol.
Figured I would first get a Cobol compiler installed. So..
# yum search cobol
Loaded plugins: langpacks, presto, refresh-packagekit
updates/pkgtags | 333 B 00:00:00
============================================= N/S Matched: cobol =============================================
open-cobol.x86_64 : OpenCOBOL - COBOL compiler
libcob.i686 : OpenCOBOL runtime library
libcob.x86_64 : OpenCOBOL runtime library
Well, this looks promising…
# yum install open-cobol
Loaded plugins: langpacks, presto, refresh-packagekit
Resolving Dependencies
--> Running transaction check
---> Package open-cobol.x86_64 0:1.1-0.20090209.fc17.2 will be installed
--> Processing Dependency: libcob = 1.1 for package: open-cobol-1.1-0.20090209.fc17.2.x86_64
--> Processing Dependency: gmp-devel >= 4.1.4 for package: open-cobol-1.1-0.20090209.fc17.2.x86_64
--> Processing Dependency: libcob.so.1()(64bit) for package: open-cobol-1.1-0.20090209.fc17.2.x86_64
--> Running transaction check
---> Package gmp-devel.x86_64 1:5.0.5-3.fc18 will be installed
---> Package libcob.x86_64 0:1.1-0.20090209.fc17.2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================
Package Arch Version Repository Size
==============================================================================================================
Installing:
open-cobol x86_64 1.1-0.20090209.fc17.2 fedora 220 k
Installing for dependencies:
gmp-devel x86_64 1:5.0.5-3.fc18 fedora 175 k
libcob x86_64 1.1-0.20090209.fc17.2 fedora 85 k
Transaction Summary
==============================================================================================================
Install 1 Package (+2 Dependent packages)
Total download size: 480 k
Installed size: 1.2 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): gmp-devel-5.0.5-3.fc18.x86_64.rpm | 175 kB 00:00:00
(2/3): libcob-1.1-0.20090209.fc17.2.x86_64.rpm | 85 kB 00:00:01
(3/3): open-cobol-1.1-0.20090209.fc17.2.x86_64.rpm | 220 kB 00:00:01
--------------------------------------------------------------------------------------------------------------
Total 342 kB/s | 480 kB 00:01
Running Transaction Check
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : 1:gmp-devel-5.0.5-3.fc18.x86_64 1/3
Installing : libcob-1.1-0.20090209.fc17.2.x86_64 2/3
Installing : open-cobol-1.1-0.20090209.fc17.2.x86_64 3/3
Verifying : open-cobol-1.1-0.20090209.fc17.2.x86_64 1/3
Verifying : libcob-1.1-0.20090209.fc17.2.x86_64 2/3
Verifying : 1:gmp-devel-5.0.5-3.fc18.x86_64 3/3
Installed:
open-cobol.x86_64 0:1.1-0.20090209.fc17.2
Dependency Installed:
gmp-devel.x86_64 1:5.0.5-3.fc18 libcob.x86_64 0:1.1-0.20090209.fc17.2
Complete!
Great, this looks awesome! Okay, now how to use it?
man -k cobol
cobol: nothing appropriate.
Um…?
$ o
obconf oldrdist openssl os-prober
obex-data-server on_ac_power openvpn osx
objcopy onsgmls openvt ownership
objdump onto2vocabularyclass orbd oxygen-demo
obxprop open orc-bugreport oxygen-gtk3-demo
oclock openal-info osd_login oxygen-gtk-demo
od openbox osgmlnorm oxygen-settings
oggconvert openbox-lxde ospam oxygen-shadow-demo
okular openbox-session ospcat
oldfind openconnect ospent
Um…?
$ co
cobc colormgr command consolehelper-gtk coproc
cob-config colrm compgen console-kit-daemon core_server.py
cobcrun column complete consoletype
col combinedeltarpm compopt continue
colcrt comm consolehelper convertquota
Ummm…?
man -k cob
cobblerd_selinux (8) - Security Enhanced Linux Policy for the cobblerd processes
httpd_cobbler_script_selinux (8) - Security Enhanced Linux Policy for the httpd_cobbler_script processes
Tcl_RegExpExecObj (3) - Pattern matching with regular expressions
Ummm..?
$ co
cobc colormgr command consolehelper-gtk coproc
cob-config colrm compgen console-kit-daemon core_server.py
cobcrun column complete consoletype
col combinedeltarpm compopt continue
colcrt comm consolehelper convertquota
Oh, that looks like it might be it.
cobc --help
Usage: cobc [options] file...
Options:
--help Display this message
--version, -V Display compiler version
-v Display the programs invoked by the compiler
-x Build an executable program
-m Build a dynamically loadable module (default)
-std=
cobol2002 Cobol 2002
cobol85 Cobol 85
ibm IBM Compatible
mvs MVS Compatible
bs2000 BS2000 Compatible
mf Micro Focus Compatible
default When not specified
See config/default.conf and config/*.conf
-free Use free source format
-fixed Use fixed source format (default)
-O, -O2, -Os Enable optimization
-g Produce debugging information in the output
-debug Enable all run-time error checking
-o
-b Combine all input files into a single
dynamically loadable module
-E Preprocess only; do not compile, assemble or link
-C Translation only; convert COBOL to C
-S Compile only; output assembly file
-c Compile and assemble, but do not link
-t
-I
-L
-l
-conf=
--list-reserved Display all reserved words
-save-temps Do not delete intermediate files
-MT
-MF
-ext
-Wall Enable all warnings
-Wobsolete Warn if obsolete features are used
-Warchaic Warn if archaic features are used
-Wredefinition Warn incompatible redefinition of data items
-Wconstant Warn inconsistent constant
-Wparentheses Warn lack of parentheses around AND within OR
-Wstrict-typing Warn type mismatch strictly
-Wimplicit-define Warn implicitly defined data items
-Wcall-params Warn non 01/77 items for CALL params (NOT set with -Wall)
-Wcolumn-overflow Warn text after column 72 (FIXED format, NOT set with -Wall)
-Wterminator Warn lack of scope terminator (END-XXX, NOT set with -Wall)
-Wtruncate Warn possible field truncation (NOT set with -Wall)
-ftrace Generate trace code (Executed SECTION/PARAGRAPH)
-ftraceall Generate trace code (Executed SECTION/PARAGRAPH/STATEMENTS)
-fsyntax-only Syntax error checking only; don't emit any output
-fdebugging-line Enable debugging lines ('D' in indicator column)
-fsource-location Generate source location code (Turned on by -debug or -g)
-fimplicit-init Do automatic initialization of the Cobol runtime system
-fstatic-linkage Statically allocate non-USING LINKAGE parameters
-fsign-ascii Numeric display sign ASCII (Default on ASCII machines)
-fsign-ebcdic Numeric display sign EBCDIC (Default on EBCDIC machines)
-fstack-check PERFORM stack checking (Turned on by -debug or -g)
-ffold-copy-lower Fold COPY subject to lower case (Default no transformation)
-ffold-copy-upper Fold COPY subject to upper case (Default no transformation)
-fnotrunc Do not truncate binary fields according to PIC (ala MF)
-fnull-param Pass extra NULL terminating pointers on CALL statements
That looks like it, but still… um…?
$ man cobc
No manual entry for cobc
I’m starting to get a little annoyed now.
info cobc
...Nothing...
Okay, let us start off by checking out the OpenCOBOL website hello world page.
So, I start out writing my program.
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
PROCEDURE DIVISION.
DISPLAY "Hello World!".
STOP RUN.
Now compile.
$ cobc -x hello.cob
hello.cob:1: Error: Invalid indicator 'I' at column 7
What? I thought I just copied and pasted stuff! After reading the doc a bit more, it becomes obvious. The cryptic message, “The I of IDENTIFICATION must be at the column 8 ( 7 spaces before ).”, really means, put 7 spaces before each line. Now this is starting to feel like Python…
Okay, so fix some spacing.
IDENTIFICATION DIVISION.
PROGAM-ID. hello.
PROCEDURE DIVISION.
DISPLAY "Hello World!"
STOP RUN.
And compile
$ cobc -x hello.cob
hello.cob:2: Error: syntax error, unexpected "Identifier", expecting "PROGRAM-ID"
Awww, snap. I didn’t copy and paste. PROGAM-ID != PROGRAM-ID. So fix some code.
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
PROCEDURE DIVISION.
DISPLAY "Hello World!"
STOP RUN.
And finally…
$ cobc -x hello.cob
In file included from /usr/include/stdio.h:27:0,
from /tmp/cob3740_0.c:6:
/usr/include/features.h:330:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
/usr/bin/ld: cannot find -lncurses
/usr/bin/ld: cannot find -ldb
collect2: error: ld returned 1 exit status
Oh, come on! Fine…
# yum install ncurses-devel
Loaded plugins: langpacks, presto, refresh-packagekit
Resolving Dependencies
--> Running transaction check
---> Package ncurses-devel.x86_64 0:5.9-7.20121017.fc18 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================
Package Arch Version Repository Size
==============================================================================================================
Installing:
ncurses-devel x86_64 5.9-7.20121017.fc18 fedora 708 k
Transaction Summary
==============================================================================================================
Install 1 Package
Total download size: 708 k
Installed size: 2.1 M
Is this ok [y/N]: u
Is this ok [y/N]: y
Downloading Packages:
ncurses-devel-5.9-7.20121017.fc18.x86_64.rpm | 708 kB 00:00:01
Running Transaction Check
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : ncurses-devel-5.9-7.20121017.fc18.x86_64 1/1
Verifying : ncurses-devel-5.9-7.20121017.fc18.x86_64 1/1
Installed:
ncurses-devel.x86_64 0:5.9-7.20121017.fc18
Complete!
Okay, try again.
$ cobc -x hello.cob
In file included from /usr/include/stdio.h:27:0,
from /tmp/cob3928_0.c:6:
/usr/include/features.h:330:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
/usr/bin/ld: cannot find -ldb
collect2: error: ld returned 1 exit status
Do some googling….
# yum install libdb-devel
Loaded plugins: langpacks, presto, refresh-packagekit
Resolving Dependencies
--> Running transaction check
---> Package libdb-devel.x86_64 0:5.3.21-3.fc18 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================
Package Arch Version Repository Size
==============================================================================================================
Installing:
libdb-devel x86_64 5.3.21-3.fc18 fedora 36 k
Transaction Summary
==============================================================================================================
Install 1 Package
Total download size: 36 k
Installed size: 126 k
Is this ok [y/N]: y
Downloading Packages:
libdb-devel-5.3.21-3.fc18.x86_64.rpm | 36 kB 00:00:00
Running Transaction Check
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libdb-devel-5.3.21-3.fc18.x86_64 1/1
Verifying : libdb-devel-5.3.21-3.fc18.x86_64 1/1
Installed:
libdb-devel.x86_64 0:5.3.21-3.fc18
Complete!
One more try!
$ cobc -x hello.cob
In file included from /usr/include/stdio.h:27:0,
from /tmp/cob4020_0.c:6:
/usr/include/features.h:330:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
Hurrah! Just one nasty warning. We’ll ignore that for now.
$ ./hello
Hello World!
Isn’t it beautiful?
-Kim