GnuCobol用参数调用C函数-当函数有两个以上的参数时出现问题

yk9xbfzb  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(100)

我有下面这样的案例

testlib.c有两个基本函数,一个返回值,另一个通过引用返回结果

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

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

编译程序库

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

prg1.cbl通过值和引用调用两个C函数“using”

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.

编译Cobol代码

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

结果

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

这是可行的当我试图调用添加到我的testlib中的带有3个参数的C函数时,问题就开始了

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

使用Cobol代码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.

在编译过程中,我得到错误

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

lpwwtiir1#

我不明白的原因是变量名的问题。将Cobol代码更改为

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.

一切顺利

相关问题