Gnu Cobol
Gnu Cobol
Table of Contents
1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 Compiler Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.1 Built Target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.2 Source Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.3 Warning Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Multiple Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 Static Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.2 Dynamic Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.3 Building Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.4 Using Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 C Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.1 Writing Main Program in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.2 Static linking with COBOL programs . . . . . . . . . . . . . . . . . . . . . . 8
2.3.3 Dynamic linking with COBOL programs . . . . . . . . . . . . . . . . . . . 9
2.3.4 Static linking with C programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.5 Dynamic linking with C programs . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Customize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1 Customizing Compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Customizing Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Optimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.1 Optimize Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2 Optimize Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3 Optimize Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 Debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.1 Debug Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
GNU Cobol Manual 1
1 Getting Started
2 Compile
This chapter describes how to compile COBOL programs using GNU Cobol.
$ cp libsubrs.so /your/cobol/lib
$ export LD_LIBRARY_PATH=/your/cobol/lib
Then, compile the main program, linking the library as follows:
$ cobc -x main.cob -L/your/cobol/lib -lsubrs
2.3 C Interface
This chapter describes how to combine C programs with COBOL programs.
int
main (int argc, char **argv)
{
/* initialize your program */
...
}
You can write cobc_init(0, NULL); if you do not want to pass command line arguments
to COBOL.
You can compile your C program as follows:
cc -c ‘cob-config --cflags‘ main.c
The compiled object must be linked with libcob as follows:
cc -o main main.o ‘cob-config --libs‘
int
main()
{
int ret;
char hello[7] = "Hello ";
char world[7] = "World!";
cob_init(0, NULL);
return ret;
}
----------------------------------------
Compile these programs as follows:
$ cc -c ‘cob-config --cflags‘ hello.c
$ cobc -c -static say.cob
$ cobc -x -o hello hello.o say.o
$ ./hello
Hello World!
int
main()
{
int ret;
char hello[7] = "Hello ";
char world[7] = "World!";
cob_init(0, NULL);
/* call the module found and exit with the return code */
ret = say(hello, world);
return ret;
}
----------------------------------------
Compile these programs as follows:
$ cc -c ‘cob-config --cflags‘ hello-dynamic.c
$ cobc -x -o hello hello-dynamic.o
$ cobc -m say.cob
$ export COB_LIBRARY_PATH=.
$ ./hello
Hello World!
putchar(’\n’);
return 0;
}
----------------------------------------
This program is equivalent to the foregoing say.cob.
Note that, unlike C, the arguments passed from COBOL programs are not terminated
by the null character (i.e., \0).
You can call this function in the same way you call COBOL programs:
---- hello.cob -------------------------
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 HELLO PIC X(6) VALUE "Hello ".
01 WORLD PIC X(6) VALUE "World!".
PROCEDURE DIVISION.
CALL "say" USING HELLO WORLD.
STOP RUN.
----------------------------------------
Compile these programs as follows:
$ cc -c say.c
$ cobc -c -static -x hello.cob
$ cobc -x -o hello hello.o say.o
$ ./hello
Hello World!
3 Customize
4 Optimize
5 Debug