我有下面这样的案例
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
1条答案
按热度按时间lpwwtiir1#
我不明白的原因是变量名的问题。将Cobol代码更改为
一切顺利