I threw together notes from an ongoing project about writing code in OCaml and turning it into a shared library usable from C: https://transactional.blog/blog/2024-calling-ocaml-from-c

If there's any #OCaml folk around who have additional tips or advice in this area, I'd love to hear it.

Calling OCaml from C

@AlexMillerDB

Curious, what is the benefit of `_caml_init` function?

I happen to write and debug some C <-> OCaml bindings and `caml_callback` always seem to go through `caml_startup` ?

@vrotgaru The docs say:
> At some point, the C code must call caml_main(argv) to initialize the OCaml code.
> The call to caml_main initializes the OCaml runtime system, loads the bytecode (in the case of the bytecode compiler), and executes the initialization code of the OCaml program.
> The C code can then invoke OCaml functions using the callback mechanism

Is that... not accurate? I saw other code using __attribute__((constructor)) to run it at global init instead.

@AlexMillerDB

It is better to raise that on discuss.ocaml.org. I just share what I saw in debugger, when trying to understand why some of my callbacks were not working.

I may look into it at some point.