首页 > 解决方案 > 对“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

标签: ocamlundefined-reference

解决方案


通过包含来自 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(但似乎也可以为空)。


推荐阅读