gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:21:47 24 4
gpt4 key购买 nike

我正在尝试从 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++ - ocaml asmrun 库中已定义的主要函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31017992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com