ocaml asmrun 库中已定义的主要函数

main function already defined in ocaml asmrun library

我正在尝试从 C 程序调用一些 ocaml 代码。我一直在关注一些文档 here。 c 程序称为 hello.c,它试图使用 callme.ml 中定义的 Ocaml 函数。

与 link 一样,我分两步进行:首先将 ml 文件编译为目标文件:

ocamlopt -output-obj -o callme2.o callme.ml

然后尝试 link 使用此代码将其添加到我的 'main' 二进制文件中:

gcc  -Wall -I`ocamlopt -where` -L`ocamlopt -where` -lasmrun -lm -ldl  -o hello hello.c callme2.o -lasmrun

但是我运行遇到以下问题:main已经在libasmrun.a中定义了,所以它和我自己hello.c中的main冲突了]:

/tmp/ccANhYNH.o: In function `main':
hello.c:(.text+0x58): multiple definition of `main'
/home/orm/.opam/4.02.0/lib/ocaml/libasmrun.a(main.o):main.c:(.text+0x0): first defined here

我该如何解决这个问题? (如库路径所示,我使用的是 ocaml 4.02 版)

更新: 这个问题更多地与正确使用 C linker 标志有关,而不是 ocaml。按以下顺序使用标志可以解决问题:

gcc  -Wall -I`ocamlopt -where` -L`ocamlopt -where` -o hello hello.c -lasmrun callme2.o -lm -ldl -lasmrun

这很有趣,因为我认为在 same program 中定义两次相同的函数名是非法的。也许这是该文档中的例外之一。

您的命令行有点奇怪,因为 -lasmrun 出现了两次。

以下是对我有用的东西:

$ W=`ocamlopt -where`
$ gcc -I $W -L $W -o hello hello.c callme.o -lasmrun -lm -ldl

你可以在我的伪博客中看到一个工作示例:Further OCaml GC Disharmony

(正如我所学到的那样,请务必遵守 GC 和谐规则 :-)