COBOL调用C函数
2023-08-21 本文已影响0人
CodingCode
COBOL和C源文件:
$ cat test1.cbl
data division.
working-storage section.
01 grp.
05 fld-1 pic x.
05 fld-2 pic 9(9) comp-5.
01 str pic x(12) value "hello world" & x'00'.
01 fmt pic x(12) value "TTTT=[%s]" & x'0d0a00'.
01 ptr-1 pointer.
01 ptr-2 pointer.
procedure division.
main.
set ptr-1 to address of str.
call "c_func_1" using by value ptr-1.
set ptr-1 to address of fld-1.
set ptr-2 to address of fld-2.
call "c_func_2" using by value ptr-1 ptr-2.
* * CALL LIBC FUNCTION printf
call "printf" using by value address of fmt by value ptr-1.
stop run returning 0.
$ cat test2.c
#include <stdio.h>
c_func_1(char * ptr) {
printf("%s\n", ptr);
}
c_func_2(void * ptr1, void * ptr2) {
printf("%d\n", ptr2 - ptr1);
}
编译运行:
$ cob -vx test1.cbl test2.c
$ ./test1
hello world
1
TTTT=[ hello world]