Following on from my “Hello, World!” COBOL program last time out I decided that it would be good to investigate how to create a program that consists of multiple source files, in other words in modular.
COBOL was originally designed when a single program was a single piece of source. However, it has for almost as long been able to split that source up into modules by using the CALL verb to get one source to use another. Let’s see how we get Tiny COBOL to build us one executable from a couple of pieces of source.
First, here’s the 2 source files in question.
*main.cob IDENTIFICATION DIVISION. PROGRAM-ID. MAIN. AUTHOR. MARK HOPKINS. DATE-WRITTEN. 21 November, 2008. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 MSG PIC X(65). PROCEDURE DIVISION. MOVE "Hello, World!" TO MSG. CALL "MSGDISP" USING MSG. STOP RUN.
*msgdisp.cob IDENTIFICATION DIVISION. PROGRAM-ID. MSGDISP. AUTHOR. MARK HOPKINS. DATE-WRITTEN. 21 November, 2008. ENVIRONMENT DIVISION. DATA DIVISION. LINKAGE SECTION. 01 MSG PIC X(65). PROCEDURE DIVISION USING MSG. DISPLAY MSG. EXIT PROGRAM.
First, let’s get them compiled up. I tried this…
$ htcobol -o hello2 main.cob msgdisp.cob Invalid number of input parameters
Seems htcobol compiles one source at a time, so we need to compile each one up to object format then link them manually. Okay here goes nothing.
$ htcobol -c main.cob $ htcobol -c msgdisp.cob
Now to get the two things joined together. To figure this out I used the -n option of htcobol which prints out all the commands that will be executed but will not actually do anything. I ran it against the single source “Hello, World!” example from last time and got this.
$ htcobol hello.cob -n Pre-processing 'hello.cob' into 'hello.i' as -o hello.o hello.s gcc -o hello hello.o -L/usr/local/lib -lhtcobol -ldl -ldb -lncurses -lm
From that I then ran this
$ gcc -o hello2 main.o msgdisp.o -L/usr/local/lib -lhtcobol -ldl -ldb -lncurses -lm
And hey presto we have an executable that when run gives our wonderful “Hello, World!” message.
$ ./hello2 Hello, World!
So we now have a way of modularising our COBOL code we may even be able to produce something useful.
Tags: cobol