Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

ocaml - undefined reference to `caml_names_of_builtin_cprim'

I'm trying to compile a tiny C program that uses the OCaml GC independently of Ocaml itself.

The code:

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

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

Compiling with:

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

Output:

/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

Any idea which library I'm missing? I've tried with all libs I could find on the system (not using or including opam):

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

The installed OCaml is:

$ which ocaml
/usr/bin/ocaml
$ ocaml -version
The OCaml toplevel, version 4.02.3
question from:https://stackoverflow.com/questions/65910143/undefined-reference-to-caml-names-of-builtin-cprim

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solved by including a dummy object file from the OCaml compiler, needed to make the linked OCaml runtime work:

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

dummy.ml contains only let i = 10 (but seems like it can be empty too).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...