Search code examples
pythonpython-2.7ffipython-cffi

Python cffi module: does verify() require re-compiling the entire c library I want to use from my python code?


Say I need to use a few functions from a shared library, e. g. libfoo.so, from python code. I figured the cffi module could help me, especially as it seems to support opaque types if one uses the verify() function.

I did not fully understand though, what the documentation means by

verify() [...] is an alternative: instead of doing a dlopen, it generates and compiles a piece of C code.

Do I have to recompile the entire libfoo.so? Or just a certain part (relevant parts of the header)? If the latter is the case, is this significantly less of a hassle in terms of dependencies, configuration options, ... ?


Solution

  • No, ffi.verify() (as well as ffi.set_source() in the soon-to-be-released cffi 1.0) generates and compiles a lightweight C wrapper which calls your existing library. The C wrapper contains regular C code that calls the library functions, so as usual with C, the library doesn't need to be recompiled in order to be used---but, also as usual with C, you need to have the headers of that library installed (typically from a package called something like libfoo-dev from your OS distribution).