Users of many popular languages bearing semantic similarity to Lisp, such as Perl and Python, are accustomed to having access to popular C libraries, such as GTK, by way of “bindings”. In Lisp, we do something similar, but take a fundamentally different approach. This tutorial first explains this difference, then explains how you can use CFFI, a powerful system for calling out to C and C++ and access C data from many Common Lisp implementations.
The concept can be generalized to other languages; at the time of writing, only CFFI’s C support is fairly complete. Therefore, we will interchangeably refer to foreign functions and foreign data, and “C functions” and “C data”. At no time will the word “foreign” carry its usual, non-programming meaning.
This tutorial expects you to have a working understanding of both Common Lisp and C, including the Common Lisp macro system.
|• Tutorial-Comparison||Why FFI?|
|• Tutorial-Getting a URL||An FFI use case.|
|• Tutorial-Loading||Load libcurl.so.|
|• Tutorial-Initializing||Call a function in libcurl.so.|
|• Tutorial-easy_setopt||An advanced libcurl function.|
|• Tutorial-Abstraction||Why breaking it is necessary.|
|• Tutorial-Lisp easy_setopt||Semi-Lispy option interface.|
|• Tutorial-Memory||In C, you collect the garbage.|
|• Tutorial-Callbacks||Make useful C function pointers.|
|• Tutorial-Completion||Minimal get-url functionality.|
|• Tutorial-Types||Defining new foreign types.|
|• Tutorial-Conclusion||What’s next?|