I’m trying to use BER MetaOCaml in a dune project. I was reading through several posts and github issues and didn’t manage to find any concrete examples. After trying many things, the following kinda works:
let string_of_code c =
let () = Codelib.print_code Format.str_formatter c in
Format.flush_str_formatter ()
let () =
let c = .<1 + 2>. in
let res = Runnative.run c in
Printf.printf "Code: %s\nResult: %d\n" (string_of_code c) res
output of dune exec bin/foo.exe:
Code: .<1 + 2>.
Result: 3
As you can see, there’s the horrible link_flags hack that scares children and adults alike. It also limits the mode to native (you could probably come up with a similar hack for byte mode). I would like to know if there’s a way to cleanly use metaocaml with dune. Thanks.
and even if I move those files and renamed them, I get the following error:
gcc: error: /home/username/.opam/4.07.1+BER/lib/metaocaml/+runnative.o: No such file or directory
gcc: error: /home/username/.opam/4.07.1+BER/lib/metaocaml/+codelib.o: No such file or directory
File "caml_startup", line 1:
Error: Error during linking
I can see those files are coming from the base-metaocaml-ocamlfind mentioned in one of the issues.
For posterity, could you describe why it wasn’t working in first place? And also, why is dynlink explicitly required in the libraries if it’s in the requires list in the META?
+ at the beginning of a directory or file name in META files means “the OCaml standard library directory”. So +metalib.cma means $(ocamlc -where)/metalib.cma. However, dune only implements this expansion for the directory field. I must have missed this part of the doc when I first implemented support for META files in dune and somehow thought it was only supported in the directory field. So technically it is a bug in Dune and we should consider fixing it.
That said, given that:
I’m pretty sure very few people rely on this feature, and indeed this is the first time I see it used
most META files are now generated by dune
META files will eventually disappear
It seems simpler to me to update the META file of metaocaml and consider the fix in Dune a low-priority one.
That’s a mistery. To be clear, it is missing when building the native executable?
Correct. This is the full error if dynlink is removed from libraries:
File "_none_", line 1:
Error: No implementations provided for the following modules:
Dynlink referenced from /home/username/.opam/4.07.1+BER/lib/ocaml/runnative.cmx