Closed ffreling closed 5 years ago
You should package your code as an executable
and set (modes object)
to generate an .o
file that can be linked into a C application, e.g.
(executable
(name hello)
(modes object))
Then doing
$ dune build hello.exe.o
will produce an .o
file that you can link into a C application.
Thank you @nojb, it indeed fixed my issue :)
I was a bit confused between executable
and library
, and I was too focused on getting a .a
output for a library.
Great, thanks!
I am trying to build an OCaml project as a C library so I can use it inside a C/C++ project. Most examples I see are linking a C library to OCaml but I want the other way around. Or they only compile a single file via an explicit call to
ocamlopt
.I tried making a simple library with dune and building it via
dune build ocaml/hello.a
:But when I link it to a C program, I have a lot of undefined internal Caml symbols
_caml_*
. If I understand correctly, that means the OCaml runtime is missing.I tried to compile sources by hand and then I was able to link by specifying the
-output-complete-obj
flag toocamlopt
:I made a sample repository to exhibit my issue: https://gitlab.com/ffreling/ocaml-native-lib Running
make clean ocaml-manual c run
links properly (although I have one remaining issue with a callback) whereas runningmake clean ocaml c run
leads to:It seems that the flag
-output-complete-obj
is not passed in dune. I looked at these links:https://github.com/ocaml/dune/blob/master/src/exe.ml#L53
but I don't understand how to express my need with the stanzas. I thought calling
dune build ocaml/hello.exe.a
would generate a library with native code but it doesn't recognize the command.Environment