我正在尝试从 C 程序调用一些 ocaml 代码。我一直在关注一些文档 here 。 c 程序名为 hello.c
,它尝试使用 callme.ml
中定义的 Ocaml 函数。
如链接中所示,我分两步执行此操作:首先将 ml 文件编译为目标文件:
ocamlopt -output-obj -o callme2.o callme.ml
然后尝试使用以下代码将其链接到我的“主”二进制文件:
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 链接器标志的正确使用有关,而不是 ocaml。按以下顺序使用标志可以解决该问题:
gcc -Wall -I`ocamlopt -where` -L`ocamlopt -where` -o hello hello.c -lasmrun callme2.o -lm -ldl -lasmrun
这很有趣,因为我认为在 same program 中定义两次相同的函数名称是非法的。 。也许这是该文档中的异常(exception)之一。
最佳答案
你的命令行有点奇怪,因为-lasmrun
出现了两次。
这是对我有用的东西:
$ W=`ocamlopt -where`
$ gcc -I $W -L $W -o hello hello.c callme.o -lasmrun -lm -ldl
您可以在我的伪博客中看到一个工作示例:Further OCaml GC Disharmony .
(正如我从惨痛的教训中学到的,请确保遵循 GC 协调规则:-)
关于c++ - main 函数已在 ocaml asmrun 库中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31017992/