c++ - main 函数已在 ocaml asmrun 库中定义

标签 c++ c ocaml ffi

我正在尝试从 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/

相关文章:

无法访问 .bss 部分中的内存,但 gdb 'info files' 显示地址在范围内

c - 为什么 syslog 和 gdb 对同一个共享库显示不同的加载地址?

ocaml - 我可以将变体类型定义为 "some other variant plus a new constructor"而无需复制粘贴垃圾邮件吗?

奥卡姆 : Function with 'a option error

if-statement - 在 OCaml 中实现多个 if 语句

c++ - QCheckbox名称访问

c++ - 如何获取和设置 CORBA 中的类字段? (处理 CORBA 对象序列)

c++ - 在 C++ 中查找紧密成员函数名称

c++ - Windows 短路径格式到长路径格式

c++ - Linux 上 Poco::Logger 的编译器错误 - 函数声明被视为宏