# Blog Post: Simple Example where Ocaml calls a C function

**URL:** <https://discuss.ocaml.org/t/blog-post-simple-example-where-ocaml-calls-a-c-function/15211>\
**Category:** Learning\
**Created:** [August 28, 2024, 10:26am UTC](https://discuss.ocaml.org/t/blog-post-simple-example-where-ocaml-calls-a-c-function/15211 "2024-08-28T10:26:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bentxt](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bentxt](https://discuss.ocaml.org/u/bentxt)\
**Post date:** [August 28, 2024, 10:26am UTC](https://discuss.ocaml.org/t/blog-post-simple-example-where-ocaml-calls-a-c-function/15211/1 "2024-08-28T10:26:59Z")

</div>

> 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.

Read the rest on [Simple Example where Ocaml calls a C function](https://srctxt.com/2024/basic-ocaml-ffi_20240828121852.html)

PS. I would like to create a follow up post that shows how to call a C function that is part of C library. Help greatly appreciated

---

<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:** [August 28, 2024, 11:11am UTC](https://discuss.ocaml.org/t/blog-post-simple-example-where-ocaml-calls-a-c-function/15211/2 "2024-08-28T11:11:51Z")

</div>

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).

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![lambda\_foo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lambda_foo/32/275_2.png) [@lambda\_foo](https://discuss.ocaml.org/u/lambda_foo)\
**Post date:** [August 30, 2024, 12:41am UTC](https://discuss.ocaml.org/t/blog-post-simple-example-where-ocaml-calls-a-c-function/15211/3 "2024-08-30T00:41:35Z")

</div>

My favourite example at the moment is this one from [Retrofitting Effect Handlers onto OCaml](https://doi.org/10.1145/3453483.3454039). It shows OCaml calling C, C calling an OCaml callback and exceptions crossing those boundaries.

```ocaml
$ 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)

```

```c
#include <caml/mlvalues.h>
#include <caml/callback.h>

value ocaml_to_c (value unit) {
    caml_callback(*caml_named_value
                  ("c_to_ocaml"), Val_unit);
    return Val_int(0);
}

```

Compile it with OCaml 5.2:

```shell
$ ocamlopt --version
5.2.0
$ ocamlopt meander_c.c meander.ml -o meander.exe
$ ./meander.exe
$ echo $?
0

```

Bonus you can use GDB/LLDB on this to set breakpoints in both OCaml and C.

---

<div class="post-metadata">

**Author:** ![bentxt](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bentxt](https://discuss.ocaml.org/u/bentxt)\
**Post date:** [August 30, 2024, 11:46pm UTC](https://discuss.ocaml.org/t/blog-post-simple-example-where-ocaml-calls-a-c-function/15211/6 "2024-08-30T23:46:12Z")

</div>

Thanks for your comment and suggestion, I updated an improved version withouth CAMLPrim, and also mentioned your comment (hope that this is OK):

[Basic Ocaml ffi with c library (update)](https://srctxt.com/2024/basic-ocaml-ffi-c-file-update_20240831013127.html)

---

<div class="post-metadata">

**Author:** ![bentxt](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bentxt](https://discuss.ocaml.org/u/bentxt)\
**Post date:** [August 30, 2024, 11:46pm UTC](https://discuss.ocaml.org/t/blog-post-simple-example-where-ocaml-calls-a-c-function/15211/7 "2024-08-30T23:46:37Z")

</div>

Thanks for your comment. I put in on my TODO list to things to study …
