已经在ocaml asmrun库中定义了main函数

main function already defined in ocaml asmrun library

本文关键字:定义 main 函数 ocaml asmrun      更新时间:2023-10-16

我正试图从C程序中调用一些ocaml代码。我一直在关注这里的一些文档。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

这很有趣,因为我认为在同一个程序中定义两次相同的函数名是违法的。也许这是该文件中的一个例外。

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

以下是对我有效的东西:

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

您可以在我的伪博客中看到一个工作示例:进一步的OCaml GC Dishamony。

(正如我所学到的,请确保遵守GC和谐的规则:-)