Ocaml Ffi Bindings
One thing that always comes up with your favourite language is how do you use libraries written in another language. Typically this involves needing to talk to a particular C library, either because it’s faster than a native one or just that it is already written.
For OCaml there is the ctypes library for binding to C libraries using pure OCaml. Written by the people at the good people at OCaml Labs http://ocaml.io
The core of ctypes is a set of combinators for describing the structure of C types — numeric types, arrays, pointers, structs, unions and functions. You can use these combinators to describe the types of the functions that you want to call, then bind directly to those functions — all without writing or generating any C!
Lets go through a simple example binding to libyaml. Here’s a declaration form libyaml to get the version string.
1 2 3 4 5 6 7 8 9 10
To bind to this we need to declare a compatible signature for our OCaml code.
1 2 3 4 5 6
We’re pulling in Ctypes and Foreign. Then the let binding is using foreign with the name of the c method we want to call plus a type signature for that method.
Next we need some calling code to print out the version string.
1 2 3 4 5
Assuming you’ve got opam installed you can get the dependencies
core ctypes and compile the whole thing.
1 2 3 4
We’ve got bindings to a native C library without writing any C.
More complicated example involving passing an allocated string back from C, lets
look at the
proc_pidpath call from OSX. This particular library call takes a
process id (PID) and returns back
To bind to this call we again define a compatible signature.
1 2 3
The arguments simply mirror those for the C library call, along with a new
check_errno which indicates the c library sets errno if it encounters
Ctypes provides native bindings for most things you’ll need. There’s all sorts of pointers and types matching pretty much every native C type you’ll need here.