Search code examples
cgnucobol

GnuCobol calling C function with arguments - problem when function has more than 2 arguments


I have case like below

testlib.c with two basic functions, one returning value second returning result by reference

int sum(int a, int b) {
    return a + b;
}

void ref(int a, int *b) {
    *b = a * a;
}

compiling library

  gcc -c -static -o testlib.o testlib.c

prg1.cbl calling both C functions "using" by value and by reference


       IDENTIFICATION DIVISION.
       PROGRAM-ID. CallCFunctions.
       
       ENVIRONMENT DIVISION.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A USAGE BINARY-SHORT SIGNED.
       01  B USAGE BINARY-SHORT SIGNED.
       01  C USAGE BINARY-SHORT SIGNED.
       01  R USAGE BINARY-SHORT SIGNED.
       

       PROCEDURE DIVISION.
           MOVE ZERO TO R.
           MOVE ZERO TO C.
           MOVE 4 TO A.
           MOVE 3 TO B.
           MOVE -1 TO C.

           CALL "sum2" USING BY VALUE A B
               RETURNING R
           DISPLAY "Sum2 R = A + B = ", R.

           MOVE ZERO TO R.
           MOVE 13 TO A.
           MOVE ZERO TO B.
           CALL "ref" USING BY VALUE A BY REFERENCE B
           
           DISPLAY "ref B = A * A = ", B.
           

       STOP RUN.

compiling Cobol code

cobc -x -free -o prg1 prg1.cbl testlib.o

Result

Sum2 R = A + B = +00007
ref B = A * A = +00169

which works. Problem starts when I try to call C function with 3 arguments, that is added to my testlib

 int sum3(int a, int b, int c) {
     return a + b + c;
 }

using Cobol code prg2.cbl

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CallCFunctions.
       
       ENVIRONMENT DIVISION.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  A USAGE BINARY-SHORT SIGNED.
       01  B USAGE BINARY-SHORT SIGNED.
       01  C USAGE BINARY-SHORT SIGNED.
       01  R USAGE BINARY-SHORT SIGNED.
       

       PROCEDURE DIVISION.
           MOVE ZERO TO R.           
           MOVE 4 TO A.
           MOVE 3 TO B.
           MOVE -1 TO C.
           CALL "sum3" USING BY VALUE A B C
               RETURNING R
           
           DISPLAY R.    

       STOP RUN.

during compilation I get error

   cobc -x -free -o prg2 prg2.cbl testlib.o
   
   prg2.cbl:19: error: syntax error, unexpected C

Solution

  • For the reason I do not understand problem was with variable names. After changing Cobol code to

           IDENTIFICATION DIVISION.
           PROGRAM-ID. CallCFunctions.
           
           ENVIRONMENT DIVISION.
    
           DATA DIVISION.
           WORKING-STORAGE SECTION.
           01  VAR-A USAGE BINARY-SHORT SIGNED.
           01  VAR-B USAGE BINARY-SHORT SIGNED.
           01  VAR-C USAGE BINARY-SHORT SIGNED.
           01  VAR-R USAGE BINARY-SHORT SIGNED.
           
    
           PROCEDURE DIVISION.
               MOVE ZERO TO VAR-R.           
               MOVE 4 TO VAR-A.
               MOVE 3 TO VAR-B.
               MOVE 1 TO VAR-C.
               CALL "sum3" USING BY VALUE VAR-A VAR-B VAR-C
                   RETURNING VAR-R
               
               DISPLAY VAR-R.    
    
           STOP RUN.
    

    It all works