Foreign Function Interface
Interop with C/C++ is the most natural option for OCaml, since the runtime is partially written in C.
intis 63-bits on 64-bit platforms and 31-bits on 32-bit platforms.
optintallows 64-bit platforms to use the native
inttype 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.
- 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.
- pyml: Library for interacting with python. Uses C stubs.
Automatically create bindings to python libraries with minimal boilerplate utilizing
- 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-ocaml-derive: provides an FFI with Rust. Works on both the OCaml-to-Rust and Rust-to-OCaml directions.
- 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.
- The OCaml manual’s chapter on interfacing C with OCaml
- Real World OCaml’s foreign function interface chapter
- Modular foreign function bindings with ctypes.