The Common Foreign Function Interface

Upstream URL


James Bielman <>, Attila Lendvai <>, Dan Knapp <>, Liam Healy <>, Francois-Rene Rideau <>


Luis Oliveira <>, Liam Healy <>



Build Status


CFFI, the Common Foreign Function Interface, purports to be a portable FFI for Common Lisp. It abstracts away the differences between the API of the native FFI's of the various Common Lisp implementations.


The CFFI library is composed of a Lisp-implementation-specific backend in the CFFI-SYS package, and a portable frontend in the CFFI package.

The CFFI-SYS backend package defines a low-level interface to the native FFI support in the Lisp implementation. It offers operators for allocating and dereferencing foreign memory, calling foreign functions, and loading shared libraries.

The CFFI frontend provides a more comfortable, declarative interface for defining foreign functions, structures, typedefs, enumerated types, etc. It is implemented in portable ANSI CL making use of the low-level operators exported by CFFI-SYS.

The CFFI-LIBFFI subsystem loads support for passing structs by value. It requires libffi for that.

Please consult the manual for further details, including installation instructions.


Please visit Github for bug reports, feature suggestions, the latest version, and to send your contributions. CFFI also has a mailing list, and a project page at



CFFI/C2FFI is an ASDF-integrated mechanism to automatically generate a complete CFFI binding from C header files.

Its input is one .h file (with possible #includes of course), and its final output is a lisp file with the relevant CFFI binding forms.

It requires a CLI tool called c2ffi, but only for the developers of the C binding libraries, not their users. c2ffi is written in C++, and it uses Clang as a library to parse the C code, and emit the result as JSON. To skip this step, these host-specific JSON files can be checked into the repos of the binding libraries. This breaks the dependence on a working c2ffi binary and the C header files, which can be a hurdle.

These JSON files are then used to automatically generate a CL file with the corresponding CFFI forms. The generated bindings mirror the C namespace into an empty CL package as closely as possible. This means that the upper/lower case of the C names are retained. It helps with reading the original docs and with rewriting C examples into lisp. #defines are also mirrored as CL defconstants.

Binding library developers are advised to introduce another package on top of this raw layer to add more lispy constructs where appropriate (e.g. with- macros that manage resources, etc).

Until CFFI/C2FFI is properly documented, you may check out these projects as examples: hu.dwim.zlib, hu.dwim.sdl, hu.dwim.bluez, and hu.dwim.mosquitto.

Related projects

  • cl-autowrap is another project that uses c2ffi to generate CFFI bindings.

Dependencies (8)

  • alexandria
  • babel
  • bordeaux-threads
  • cl-json
  • cl-ppcre
  • rt
  • trivial-features
  • uiop

Dependents (270)

  • GitHub
  • Quicklisp