What cobc does
To get a better feel for exactly what cobc does, add the -v
and -save-temps
compiler flags to your command line. Follow the output, and step through the separate processes of:
- Precompiling the COBOL program
- Translating the COBOL program to "C"
- Compiling the "C" program
- Linking the executable object (shared object/DLL or native executable)
- Embedding the manifest file in the executable object
After examining the two cases of created a DLL, and a native executable in Windows, we will examine a third case, in which cobc is used to compile a "C" program, and explain what is taking place "under the hood".
For the purpose of this exercise, we have created a table, with the output in the column on the left, and commentary in the column on the right.
Creating a shared object/dll (Windows)
Output | Description | |
---|---|---|
1 | C:\lab4>cobc -v -save-temps hello.cbl | Compiling hello.cbl with –v and –save-temps .–save-temps causes a subfolder called “c” to be created, in which intermediate files are stored, in the step of translating COBOL to “C”. The absence of any other compiler options will assume the default of –m , and create a shared object/DLL as the executable object. |
2 | cobc:0: Temps files to 'C:\lab4\c' | When translating COBOL to “C”,intermediate files will be stored in a “c” subfolder. |
3 | cobc:0: preprocessing C:\lab4\hello.cbl into C:\lab4\c/hello.i |
cobc begins by preprocessing the COBOL program, expanding copy files, setting constant values that have been defined in level 78s, applying conditional compile directives, and so forth. The preprocessed COBOL program is stored in a temporary COBOL program file with a .i extension |
4 | cobc:0: Processing hello | Informational message marking the transition from preprocessing to processing. |
5 | cobc:0: translating C:\lab4\c/hello.i into C:\lab4\c/hello.c |
cobc translates the preprocessed COBOL program into “C”. Note that cobc can be directed to stop here, using the command>cobc –C hello.cbl |
6 | cobc:0: cl /c /I C:\COBOL\CobolIT64\include /DCOB_HAS_THREAD /W0 /nologo /GF /MD /Fo"hello.obj" "C:\lab4\c/hello.c" |
cobc compiles the “C” program to an .obj file in the current directory. These are the default flags passed to the “C” compiler, and can be changed by changing the compiler environment variable COB_CFLAGS .Note that on Windows platforms, the default setting for COB_CFLAGS is: /I C:\Cobol\CobolIT\include /DCOB_HAS_THREAD /W0 /nologo /GF /MD . These are the flags added after “cl /c” and before /Fo “hello.obj” [c file]. |
7 | hello.c | Informational message. |
8 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
9 | cobc:0: link /DLL /MANIFEST /out:"hello.dll" /nologo "hello.obj" C:\COBOL\CobolIT64\lib\libcobit_dll.lib /DEFAULTLIB:MSVCRT.LIB |
Links hello.obj with libcobit_dll.lib . Creates output files hello.lib , hello.exp , hello.dll.manifest , hello.dll . |
10 | Creating library hello.lib and object hello.exp | Informational message |
11 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
12 | cobc:0: mt /nologo /manifest "hello.dll.manifest" "/outputresource:hello.dll;#2" | Embeds the manifest file in the compiled object. |
13 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
Creating an executable (.exe) (Windows)
Output | Description | |
---|---|---|
1 | C:\lab4>cobc -v -save-temps -x hello.cbl | Compiling hello.cbl with –v –save-temps-x .–save-temps causes a subfolder called “c” to be created, in which intermediate files are stored, in the step of translating COBOL to “C”. It also ensures that intermediate files in the working directory will be saved. A native executable will be created by the compiler. |
2 | cobc:0: Temps files to 'C:\lab4\c' | When translating COBOL to “C”, intermediate files will be stored in a “c” subfolder. |
3 | cobc:0: preprocessing C:\lab4\hello.cbl into C:\lab4\c/hello.i |
cobc begins by preprocessing the COBOL program, expanding copy files, setting constant values that have been defined in level 78s, applying conditional compile directives, and so forth. The preprocessed COBOL program is stored in a temporary COBOL program file with a .i extension. |
4 | cobc:0: Processing hello | Informational message marking the transition from preprocessing to processing. |
5 | cobc:0: translating C:\lab4\c/hello.i into C:\lab4\c/hello.c |
cobc translates the preprocessed COBOL program into “C”. Note that cobc can be directed to stop here, using the command>cobc –C hello.cbl |
6 | cobc:0: cl /c /I C:\COBOL\CobolIT64\include /DCOB_HAS_THREAD /W0 /nologo /GF /MD /Fo"hello.obj" "C:\lab4\c/hello.c" |
cobc compiles the “C” program to an .obj file in the current directory. These are the default flags passed to the “C” compiler, and can be changed by changing the compiler environment variable COB_CFLAGS . Note that on Windows platforms, the default setting for COB_CFLAGS is: /I C:\Cobol\CobolIT\include /DCOB_HAS_THREAD /W0 /nologo /GF/MD . These are the flags added after “cl /c” and before /Fo “hello.obj” [c file]. |
7 | hello.c | Informational message |
8 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
9 | cobc:0: Building main entry point | Generates hello_main.c in the current directory. |
10 | cobc:0: cl /c /I C:\COBOL\CobolIT64\include /DCOB_HAS_THREAD /W0 /nologo /GF /MD /Fo"hello_main.obj" "hello_main.c" |
Compiles hello_main.c to hello_main.obj using COB_CFLAGS (or default settings, as in this case). |
11 | hello_main.c | Informational message. |
12 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
13 | cobc:0: link /SUBSYSTEM:CONSOLE /MANIFEST "/out:hello.exe" /nologo "hello.obj" C:\COBOL\CobolIT64\lib\libcobit_dll.lib "hello_main.obj" /DEFAULTLIB:MSVCRT.LIB |
Creates hello.lib , hello.exp , hello.exe , hello.exe.manifest . |
14 | Creating library hello.lib and object hello.exp | Informational message. |
15 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
16 | cobc:0: mt /nologo /manifest hello.exe.manifest /outputresource:hello.exe;#2 |
Embeds the manifest file in the executable hello.exe . |
17 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
Compiling a "C" program with cobc (Windows)
In the Interoperability topics, we see examples of using cobc to compiler "C" programs. This may seem odd at first, but it is important to remember that cobc translates COBOL into "C", and then invokes the host "C" compiler. When compiling a "C" program, cobc skips the COBOL preprocessing, and translation steps, and proceeds directly to the "C" compilation step. Again, let's examine what that looks like in an example, using the -v
and -save-temps
compiler flags.
Output | Description | |
---|---|---|
1 | C:\lab4>cobc -v -save-temps say.c | Compiling say.c with –v and –save-temps .–save-temps causes a subfolder called “c” to be created. Note that in this case, no intermediate files will be created in the “c” subdirectory. However, the –save-temps compiler flag will prevent the .obj file, which is referenced in the –v output, from being deleted. The absence of any other compiler options will assume the default of –m , and create a shared object/DLL as the executable object. |
2 | cobc:0: Temps files to 'C:\lab4\c' | The “c” subfolder is created, but no files are created in it. |
3 | cobc:0: Processing say | Informational message. Note that the preprocessing and translation steps were skipped, as there was no COBOL to preprocess or translate into “C”. |
4 | cobc:0: cl /c /I C:\COBOL\CobolIT64\include /DCOB_HAS_THREAD /W0 /nologo /GF /MD /Fo"say.obj" "C:\lab4\say.c" |
cobc compiles the “C” program to an .obj file in the current directory. These are the default flags passed to the “C” compiler, and can be changed by changing the compiler environment variable COB_CFLAGS . Note that on Windows platforms, the default setting for COB_CFLAGS is: /I C:\Cobol\CobolIT\include /DCOB_HAS_THREAD /W0 /nologo /GF/MD . These are the flags added after “cl /c” and before /Fo “hello.obj” [c file]. |
5 | say.c | Informational message. |
6 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
7 | cobc:0: link /DLL /MANIFEST /out:"say.dll" /nologo "say.obj" C:\COBOL\CobolIT 64\lib\libcobit_dll.lib /DEFAULTLIB:MSVCRT.LIB |
Links say.obj with libcobit_dll.lib . Creates output files say.lib , say.exp , say.dll.manifest , say.dll . |
8 | Creating library say.lib and object say.exp |
Informational message |
9 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |
10 | cobc:0: mt /nologo /manifest "say.dll.manifest" "/outputresource:say.dll;#2" |
Embeds the manifest file in the compiled object. |
11 | cobc:0: Exit code = 0 | Informational message. The operation was successful. |