Gnucobol 将 .cob 编译为 .c 未能通过 clang 的编译器

Gnucobol compiles .cob to .c failed to pass compiler of clang

我试图将 hello.cob 编译为 hello.c:

$ ls
hello.cob
$ cobc -x -C hello.cob
$ ls
hello.c  hello.c.h  hello.c.l.h  hello.cob

但是clang无法编译hello.c为可执行文件:

$ clang hello.c
/tmp/hello-479acf.o: In function `main':
hello.c:(.text+0x1e): undefined reference to `cob_init'
hello.c:(.text+0x2a): undefined reference to `cob_stop_run'
/tmp/hello-479acf.o: In function `sampleCOBOL_':
hello.c:(.text+0x98): undefined reference to `cob_module_global_enter'
hello.c:(.text+0x133): undefined reference to `cob_display'
hello.c:(.text+0x13f): undefined reference to `cob_stop_run'
hello.c:(.text+0x15a): undefined reference to `cob_check_version'
hello.c:(.text+0x33d): undefined reference to `cob_set_cancel'
hello.c:(.text+0x38e): undefined reference to `cob_fatal_error'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这里是hello.cob(可以编译cobc -x hello.cob):

      IDENTIFICATION DIVISION.
      PROGRAM-ID. sampleCOBOL.
      PROCEDURE DIVISION.
      DISPLAY "Hello World!".
      STOP RUN.

我很确定 clang 不会编译 C 源代码失败,因为您没有任何 C 编译器警告或错误。 它不能 link 生成的目标文件,因为你没有告诉 clang link 反对 libcob 并且没有 "magic" 让 clang 知道在哪里可以找到它的符号。将 -lcob 添加到您的 clang 调用中可能已经足够了。

如果您想知道 cobc 如何调用 compiler/linker,请将 -v 添加到您的 cobc 调用中。

注意:如果此版本的 cobc 是使用 gcc 构建的,它也默认使用 gcc。您可以使用 cobc --info 查看这一点,它还显示是否有任何 built-in 命令被环境变量覆盖。 附加说明:cobc 不仅调用 C compiler/linker,它还会生成特定于编译器的 C 代码。关于 C 代的最重要的部分是 -f[no-]computed-goto,以防 C 编译器抱怨(在你的情况下它不会)。