Dune and embedding OCaml byte code

Hi all,

I’ve written a transpiler in OCaml and would like to embed it in an application. Pretty sure I get the gist of this from Real World OCaml however I’ve been fiddling around with dune and can’t seem to figure out how to get this working. Can anyone point me to an example of this that uses dune, preferably to -output-obj a c file linkable with the OCaml runtime?

Many thanks,

It would be easier to help you if you could show us some code?

Could you give us some more details of your design?

  • What code is in control? Is the app calling the OCaml code or vice versa?
  • What is the relevance of the bytecode? Is it loaded at runtime?

I’m not sure about your needs. Maybe, this repository may help.

Best regards

Many thanks, this is very helpful! I’ve forked your repo and extended the demo to illustrate doing the same via an object file (repo). If you would like me to make a pull request let me know.

What I’d really like is to get dune to generate a c file with the OCaml bytecode via -output-obj filename.c so that I can ship this around and only need to link to libcamlrun. I’m pretty sure however that dune doesn’t support this currently.

Thanks again,

Certainly not. But it may be possible to write a (rule ) to generate the C file?

In my old cludgie Makefile approach the only change needed would be to modify:

ocamlc ... -output-obj filename.o


ocamlc ... -output-obj filename.c

This is the first project I’ve tried using dune so take with a grain of salt but I think a mode such as “cfile” and an “%{ext_c}” extension would be needed for this to be simple. I’m sure this could be done using rules in several dune files if you know dune well enough.

I’ve created a simple example of why one might be interested in using ocamlc to output C code containing OCaml bytecode (repo).

Maybe, you should ask directly dune developers by opening a ticket on github?

See pull request #1229.

Nice. I did not spot it.