Ctypes is a library for binding to C libraries using pure OCaml. The primary aim is to make writing C extension as straightforward as possible. The current foreign function interface (FFI) in OCaml is low-level and difficult to use correctly, so we’re keen to improve the ease-of-interoperability via the OCaml Platform.
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!
- Ctypes README and wiki
- Chapter 19: Foreign Function Interface of Real World OCaml
- Blog: Modular Foreign Function Bindings in the context of MirageOS
- Blog: Type-safe C bindings using ocaml-ctypes and stub generation introduces the Cstubs interface
- Tutorial: Using Cstubs_structs provides details on how to use the module to reliably determine data layout