How to compile OCaml and C/C++ code that depend on each other?

There are some examples in my lablqml (tag=0.6.2) repo:

  • startup from OCaml and C++
  • building using Makefile/ocamlbuild/dune
  • linking Qt as C++ libraries

You can try to look at that and I can help you with compilation issues (if they will appear).


There is a (I think) relatively new option for ocamlopt available, namely the -output-complete-obj flag. This is not mentioned in the manual, nor is it brought up by ‘man ocamlopt’, but ‘ocamlopt -help’ does reveal its presence. Its purpose seems to be to provide a fully linked-up object file which would avoid some of the gymnastics when linking up an executable which has a C main entry point.

Possibly it is experimental, I don’t know, but it doesn’t seem to work well with a native build. Dune’s object mode utilizes that flag, but including a link_flags field in an executable stanza for a native object build will fail if the build tries to link explicitly to a system shared object file, giving an error message that the file cannot be found. This dune file fails with “ld: cannot find -lpthread” because of the attempt to include ocaml’s thread support (libpthread is available, contrary to what is said). Possibly there is some sandboxing going on, dunno.

  (name mylib)
  (libraries threads)
  (modes native object))