# OCaml FFI for interoperability with C

**URL:** <https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410>\
**Category:** Community\
**Tags:** c, ocaml, compilation, ffi\
**Created:** [February 27, 2019, 9:33am UTC](https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410 "2019-02-27T09:33:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![p3rsik](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@p3rsik](https://discuss.ocaml.org/u/p3rsik)\
**Post date:** [February 27, 2019, 9:33am UTC](https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410/1 "2019-02-27T09:33:47Z")

</div>

Hello.  
I’m trying to link OCaml with unit tests in C and got a problem with registering callback.  
When I try to get registered callback from OCaml in C(using `caml_named_value()`) I get null pointer.  
Compiling OCaml with:  
`ocamlfind ocamlopt -o out.o -package num,core -output-obj -thread -linkpkg $(shell ocamldep -sort $(TEMP_DIR)/*.mli $(TEMP_DIR)/*.ml)`  
Then linking with C using:  
`gcc -o ex *.c out.o -Wall -DCAML_NAME_SPACE=caml -lpthread -I`ocamlc -where`-I.. -I. -L`ocamlopt -where`-lunix -lm -ldl -lbigarray -lthreadsnat -lasmrun -L`opam var num:lib`-lnums`  
If I need to post any other specification (e.g. of my pc, versions) please let me know.

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [February 27, 2019, 9:57am UTC](https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410/2 "2019-02-27T09:57:24Z")

</div>

Obvious question: did you check that `Callback.register` has been executed?

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [February 27, 2019, 4:05pm UTC](https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410/3 "2019-02-27T16:05:36Z")

</div>

When you compile OCaml code with `-output-obj`, you are responsible for calling `caml_startup` to initialize the Caml runtime and execute any initialization code. See Sec 20.7.5 of the manual [https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html](https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html) for more info.

---

<div class="post-metadata">

**Author:** ![p3rsik](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@p3rsik](https://discuss.ocaml.org/u/p3rsik)\
**Post date:** [February 27, 2019, 5:38pm UTC](https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410/4 "2019-02-27T17:38:32Z")

</div>

How can I check this?

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [February 27, 2019, 6:20pm UTC](https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410/5 "2019-02-27T18:20:41Z")

</div>

> [@Kakadu](#):
>
> `Callback.register`

For example, you find code where `Callback.register` is called and add printf there.  
As @nojb said, this code will probably not beingh executed unless you did `caml_startup` from the C side.

---

<div class="post-metadata">

**Author:** ![p3rsik](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@p3rsik](https://discuss.ocaml.org/u/p3rsik)\
**Post date:** [March 1, 2019, 1:51pm UTC](https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410/6 "2019-03-01T13:51:43Z")

</div>

I did call caml\_startup in my main C function and I already added printf to check if it’s being called, but nothing gets printed in stdout. So what am I doing wrong here, any ideas?

---

<div class="post-metadata">

**Author:** ![jeffsco](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jeffsco/32/45_2.png) [@jeffsco](https://discuss.ocaml.org/u/jeffsco)\
**Post date:** [March 1, 2019, 5:47pm UTC](https://discuss.ocaml.org/t/ocaml-ffi-for-interoperability-with-c/3410/7 "2019-03-01T17:47:56Z")

</div>

(Just a note to make sure you’re flushing stdout before drawing conclusions. I have learned this the hard way. A reasonably nice way to flush stdout is to use the %! specifier:

```
Printf.printf "This location was reached\n%!"

```

J)
