Foreign Function Interface
Interop with C/C++ is the most natural option for OCaml, since the runtime is partially written in C.
Miscellaneous
- optint:
OCaml’s built-in
int
is 63-bits on 64-bit platforms and 31-bits on 32-bit platforms.optint
allows 64-bit platforms to use the nativeint
type for 32-bit values, while boxing on those platforms that need it (32-bit). This is extremely useful for high-performance interop with other languages, network protocols etc.
C FFI
- ctypes: Modern library for binding to C libraries using pure OCaml. You should always try to bind to C code using ctypes before trying the older method.
- ppx_cstubs: Possibly the easiest solution for FFI with C, allowing you to place C code in your OCaml files. Everything else is taken care of for you.
- cstruct: Library to access C-style structures from OCaml. It comes with a ppx preprocessor and variants for lwt and async.
- The older way of binding to C code is to create stubs in C which call functions in the OCaml runtime. This method is error prone, and you’re advised to stay away from it unless it’s absolutely necessary.
- ocaml-main-program-in-c – Example build system for making mixed C/Ocaml binaries where the main program is in C.
Python FFI
- pyml: Library for interacting with python. Uses C stubs.
- pyml_bindgen:
Automatically create bindings to python libraries with minimal boilerplate utilizing
pyml
. - OCaml-in-Python: Easily call OCaml code from Python.
- Py: Library for interacting with Python 3.5 using Ctypes.
- lymp: Another library for interacting with Python, this one using a Python process.
Rust FFI
- rust-ocaml-derive: provides an FFI with Rust. Works on both the OCaml-to-Rust and Rust-to-OCaml directions.
Others
- OCaml-lua: Bindings to lua 5.1. Lua is a very efficient and light dynamic language that’s often embedded in performant applications.
- OCaml-R: FFI to the R statistical language. Docs.
Articles
- The OCaml manual’s chapter on interfacing C with OCaml
- Real World OCaml’s foreign function interface chapter
- Modular foreign function bindings with ctypes.