-2

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
mysiar
  • 430
  • 1
  • 5
  • 14
  • 6
    You should move your solution to an answer (answering your own question) and accept it. That way it is clear that this page contains a solution to the question in the title, not just an unsolved problem. – Fritz Aug 30 '23 at 12:14
  • 2
    two hints: 1. compile with `-static` - otherwise those two programs are needlessly resolved at run time (or `-k sum2 -k ref`, if you want some CALL literal to be resolved at compile time, while you want others to be "looked up") - or, if you want to stick with GnuCOBOL use `CALL STATIC "sum2"` to make that explicit and don't specify that on command line. 2. to ensure that the second does not try to update the `RETURN-CODE` register: use `CALL "ref2" USING .... RETURNING NOTHING`. – Simon Sobisch Aug 30 '23 at 12:58

1 Answers1

0

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

mysiar
  • 430
  • 1
  • 5
  • 14
  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - [From Review](/review/low-quality-posts/34927671) – OznOg Aug 31 '23 at 17:13