Absent Member.
Absent Member.

compiling .cbl programs to create .o and use in batch script

We have a set of .cbl files in the Linux environment. Have installed Visual COBOL for Eclipse for SUSE.

I need to compile the source code and use the executable in the script to run the batch jobs.

Will executing the command "cob -z -P Program1.cbl" create a .o file and can we use the .o executable in my script to run the batch like



/location/program1.o unix >>outputfile


Is the approach fine? Since I am a novice, can someone please guide me step by step of how to go ahead executing it.

5 Replies
Absent Member.
Absent Member.

.o files are object files - they contain programs compiled to native code (that is, machine code specific to a particular platform) but in a form that is not quite ready to run. For the operating system to be able to run the program it must first be linked to form either an executable (which can be run by the OS) or a shared object (which contains a library of programs ready to be called by other programs).

You can use "cob -x" to create an executable or "cob -z" to create a shared object - see the topic "Compiling and Linking from the Command Line" in the Visual COBOL documentation for more on using "cob".

It looks as though you want to create a program you can run, so you would want

cob -x Program1.cbl

to create an executable file simply called "Program1" (If you are used to running on Windows, remember that UNIX is case-sensitive so "Program1" is not the same as "program1"). Then you can run it:


This is the simplest case, running in the current directory with no arguments. To adapt your script snippet above:

export DD_SYS001=input_file
/location/Program1 unix >> outputfile

Absent Member.
Absent Member.

Thank you Jon for the explanation.

I have a set of programs which needs to be run in SUSE Linux with the help of a shell script.
For that I am using Visual COBOL for Eclipse to create executable, but was not sure what kind of executable will it give.

Have created a script as below to compile the programs.

for i in `ls *.cbl`
echo "Begin Compiling $i"
cob -xcvPC "DIRECTIVES=/home/satya/options" $i
if [ $ReturnCode -ne 0 ]
echo "Error detected during cob of $i" >> COMPALL.err
echo "Compile completed successsfully for $i" >> COMPALL.err
rm $i

Will this work fine to successfully create executable
Absent Member.
Absent Member.

Let's look at the options you are using to compile each source file, as found in the 'cob flags' topic of the Visual COBOL documentation:
cob -xcvPC "DIRECTIVES=/home/satya/options"
-x (compile to executable)
-c (compile no further than object file)
-v (verbose)
-P (produce a listing file)
-C "DIRECTIVES=/home/satya/options" (pass this DIRECTIVES setting to the syntax checker)

So this will produce the right kind of object files to be linked into an executable, but not link them - it will produce the object files along with listing files, and produce verbose output as it does so. It will also look for compiler directives in /home/satya/options.

So you will still need to link your object files to be able to run them. How to do this depends what you need:

If you want a separate executable produced from each source file, simply delete the "c" option that stops it producing executables in the first place. So change that line to
cob -xcvPC "DIRECTIVES=/home/satya/options" $i
and the compiler will produce an executable from each .cbl source file.

If you want to produce a single executable from all the files compiled, you will need to do that after compiling them all to object code - so keep the loop as you have it to produce the object files, then after the loop add a line
cob -xve Program1 *.o
(where Program1 is the name of the main program for your executable)
to build the executable.
Absent Member.
Absent Member.

Thank you for the confirmation. I will be needing the executable separately. So will do it like
cob -xcvPC "DIRECTIVES=/home/satya/options" $I

I am planning to link them like below:

linkpgm program1 copybook1 >> COMPALL.err
linkpgm program2 copybook1 copybook2 >> COMPALL.err

Please let me know if the approach if good to go.

Then will be using the executable in the script to run in batch script.
Absent Member.
Absent Member.

A couple of points on the script itself first:
If you want to make sure the script is run using bash, that first line should be
Remember that UNIX is case-sensitive, so $I is not the same as $i.

Now the linking question. I'm not sure what linkpgm is - is that a script you are using?

To link a program (compiled to object code, program1.o) and some subprograms (compiled to object code, subprog1.o and subprog2.o) into an executable using cob:
cob -x program1.o subprog1.o subprog2.o

(this is the simplest version - you can add other options like -v as above)

It might be helpful to create a very simple COBOL program (or use one of the samples included with Visual COBOL) and practice compiling, linking and running that from the command line so you can get used to how that works before you use it in your script.
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.