Calling C functions
| Linking C and COBOL | |
| The CALL sentence | |
| Return values |
Linking C and COBOL
GnuCOBOL links with C objects directly. Compile the C file with gcc, compile the COBOL with cobc -c, and link both in one step:
gcc -c helper.c -o helper.o
cobc -x -free -o main main.cob helper.o
The CALL sentence
CALL names the C function, USING lists the arguments in order, and RETURNING takes the value. C pointers map to COBOL BY REFERENCE items, and plain values to BY VALUE:
CALL "c_sqrt" USING BY VALUE WS-N
RETURNING WS-RESULT.
Return values
Keep the C side simple for COBOL callers: pass everything through the USING list, keep one return value, and document whether each argument is BY VALUE or BY REFERENCE in the copybook header.
Next: Subprograms: CALL and USING
Article author: Arthur Isaev