Compiling C lib and OCaml exe using this, all using ocamlfind

I am trying to decide how to use ocamlfind to compile the C library and the OCaml executable using this C library.

I put together a collection of pretty dumb example files.

 % cat sillystubs.c #include <stdio.h> #include <caml/mlvalues.h> #include <caml/memory.h> #include <caml/alloc.h> #include <caml/custom.h> value caml_silly_silly( value unit ) { CAMLparam1( unit ); printf( "%s\n", __FILE__ ); CAMLreturn( Val_unit ); } % cat silly.mli external silly : unit -> unit = "silly_silly" % cat foo.ml open Silly open String let _ = print_string "About to call into silly"; silly (); print_string "Called into silly" 

I believe the following way is to build the library:

 % ocamlfind ocamlc -c sillystubs.c % ar rc libsillystubs.a sillystubs.o % ocamlfind ocamlc -c silly.mli % ocamlfind ocamlc -a -o silly.cma -ccopt -L${PWD} -cclib -lsillystubs 

Now I seem to be unable to use the created library:

 % ocamlfind ocamlc -custom -o foo foo.cmo silly.cma /usr/bin/ld: cannot find -lsillystubs collect2: ld returned 1 exit status File "_none_", line 1, characters 0-1: Error: Error while building custom runtime system 

OCaml's tools are somewhat mysterious to me, so any pointers would be most welcome.

+4
source share
3 answers

ocamlmklib to the rescue. Step by step:

 $ ocamlc -verbose -c sillystubs.c $ ocamlmklib -verbose sillystubs.o silly.mli -o silly $ ocamlc -verbose silly.cma foo.ml -o foo File "foo.ml", line 1, characters 0-1: Error: Error while linking foo.cmo: The external function `silly_silly' is not available 

Oops, you defined caml_silly_silly in sillystubs.c but you refer to silly_silly in silly.mli (so stupid :) Repairing:

 $ cat silly.mli external silly : unit -> unit = "caml_silly_silly" $ ocamlmklib -verbose sillystubs.o silly.mli -o silly $ ocamlc -custom -verbose silly.cma foo.ml -o foo /usr/bin/ld: cannot find -lsilly collect2: ld returned 1 exit status File "foo.ml", line 1, characters 0-1: Error: Error while building custom runtime system 

Still out of luck? Add -I. to find the required libraries.

 $ ocamlc -I . -verbose silly.cma foo.ml -o foo.byte $ ocamlc -I . -verbose -custom silly.cma foo.ml -o foo.byte.custom $ ocamlopt -I . -verbose silly.cmxa foo.ml -o foo.native 

But in the โ€œrealโ€ setup, you really want to install the stupid library with ocamlfind, and then compile with ocamlfind, add the necessary command line parameters and everything will work automatically. Starting from scratch, the whole procedure is as follows:

 $ ocamlc -verbose -c sillystubs.c $ ocamlmklib -verbose sillystubs.o silly.mli -o silly $ cat META version="0" description="quite silly" archive(byte)="silly.cma" archive(native)="silly.cmxa" $ ocamlfind install silly META silly.cm* *.mli *.a *.so Installed /usr/local/lib/ocaml/3.11.2/silly/silly.a Installed /usr/local/lib/ocaml/3.11.2/silly/libsilly.a Installed /usr/local/lib/ocaml/3.11.2/silly/silly.mli Installed /usr/local/lib/ocaml/3.11.2/silly/silly.cmxa Installed /usr/local/lib/ocaml/3.11.2/silly/silly.cmi Installed /usr/local/lib/ocaml/3.11.2/silly/silly.cma Installed /usr/local/lib/ocaml/3.11.2/silly/META Installed /usr/local/lib/ocaml/3.11.2/stublibs/dllsilly.so Installed /usr/local/lib/ocaml/3.11.2/stublibs/dllsilly.so.owner $ rm *.cm* *.a *.so *.o $ ocamlfind ocamlopt -linkpkg -package silly foo.ml -o foo.native $ ocamlfind ocamlc -custom -linkpkg -package silly foo.ml -o foo.byte.custom $ ocamlfind ocamlc -linkpkg -package silly foo.ml -o foo.byte 

Ready source and byte versions. BTW ocamlc -custom out of date .

The mystery presented, I hope.

+7
source

OCaml tools are a little mysterious to me,

I think so too. why don't you use omake? omake simplifies the build sequence. http://omake.metaprl.org/manual/omake-build.html#@fun272

+1
source

I am an OCaml version, but it looks like silly.cma has sillystubs linked dynamically, so you might need

 -ccopt -L${PWD} 

in this last line.

In addition, I donโ€™t see anything that you are doing, which requires additional ocamlfind power - you can also call ocamlc directly.

+1
source

Source: https://habr.com/ru/post/1309367/


All Articles