微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对 `caml_names_of_builtin_cprim' 的未定义引用

如何解决对 `caml_names_of_builtin_cprim' 的未定义引用

我正在尝试编译一个独立于 Ocaml 本身使用 OCaml GC 的微型 C 程序。

代码

#include <caml/mlvalues.h>
#include <caml/memory.h>

void foo(value v1,value v2,value v3)
{
    CAMLparam3 (v1,v2,v3);
    CAMLreturn0;
}

编译:

gcc -L/usr/lib/ocaml -lcamlrun -lm -ldl -lcamlstr src/benchmarks/binarytrees-escaped.c

输出

/usr/lib/ocaml/libcamlrun.a(startup.o): In function `caml_main':
(.text+0x62c): undefined reference to `caml_names_of_builtin_cprim'
/usr/lib/ocaml/libcamlrun.a(startup.o): In function `caml_main':
(.text+0x6b2): undefined reference to `caml_names_of_builtin_cprim'
/usr/lib/ocaml/libcamlrun.a(dynlink.o): In function `caml_build_primitive_table':
(.text+0x2aa): undefined reference to `caml_builtin_cprim'
/usr/lib/ocaml/libcamlrun.a(dynlink.o): In function `caml_build_primitive_table':
(.text+0x2ba): undefined reference to `caml_names_of_builtin_cprim'
/usr/lib/ocaml/libcamlrun.a(dynlink.o): In function `caml_build_primitive_table':
(.text+0x338): undefined reference to `caml_names_of_builtin_cprim'
/usr/lib/ocaml/libcamlrun.a(dynlink.o): In function `caml_build_primitive_table_builtin':
(.text+0x3ad): undefined reference to `caml_builtin_cprim'
/usr/lib/ocaml/libcamlrun.a(dynlink.o): In function `caml_build_primitive_table_builtin':
(.text+0x3cf): undefined reference to `caml_builtin_cprim'
collect2: error: ld returned 1 exit status

知道我缺少哪个库吗?我已经尝试了我能在系统上找到的所有库(不使用或包括 opam):

/usr/lib/ocaml/libcamlrun.a
/usr/lib/ocaml/libcamlrun_pic.a
/usr/lib/ocaml/libcamlrun_shared.so
/usr/lib/ocaml/libcamlstr.a

安装的OCaml是:

$ which ocaml
/usr/bin/ocaml
$ ocaml -version
The OCaml toplevel,version 4.02.3

解决方法

通过包含来自 OCaml 编译器的虚拟对象文件解决,需要使链接的 OCaml 运行时工作:

ocamlopt -output-obj -o test.o dummy.ml
cc src/benchmarks/binarytrees-escaped.c test.o -L`ocamlc -where` -lasmrun -lm -ldl

dummy.ml 仅包含 let i = 10(但似乎也可以为空)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。