While there a great deal of techniques and ressources that help to connect Ocaml with C, I had hard time to find a very simple example that shows how you can call a C function from Ocaml.
Looks good! It is always useful to have these kinds of simple examples available.
Just a tiny comment: the CAMLprim macro is something used in the compiler codebase, but does not actually do anything for user code (even if some people like to keep it anyway as a kind of reminder that the function is exposed as an OCaml primitive).
My favourite example at the moment is this one from Retrofitting Effect Handlers onto OCaml. It shows OCaml calling C, C calling an OCaml callback and exceptions crossing those boundaries.
$ cat meander.ml
external ocaml_to_c
: unit -> int = "ocaml_to_c"
exception E1
exception E2
let c_to_ocaml () = raise E1
let _ = Callback.register
"c_to_ocaml" c_to_ocaml
let omain () =
try (* h1 *)
try (* h2 *) ocaml_to_c ()
with E2 -> 0
with E1 -> 42
let _ = assert (omain () = 42)