detly detly - 4 months ago 24
Python Question

How can use CFFI to call an existing C function given the source code?

I have a C source/header file that are part of a bigger project. I would like to test this as a unit, independent of the real project. While it would be possible to do this in C by creating a new project with a different

, I would like to see if I can use Python (3) and its frameworks (eg. nose) to accelerate the construction of tests, use existing reporting frameworks, etc.

I was under the impression that I could do this with CFFI. Here's a sample C file:

// magic.c
// Implementation of magic.
int add(int a, int b)
return a;

The header:

// magic.h
// Add two numbers (where a + b is not greater than INT_MAX).
int add(int a, int b);

Here's a script that just tries to compile it so I can call some functions:

import cffi


SOURCES = ('magic.c',)

ffi = cffi.FFI()

'#include "magic.h"',
include_dirs = INCLUDE_DIRS,
sources = SOURCES,
libraries = [],


Ultimately I plan to have this be part of the setup before a set of unit tests eg. a pure Python function
will call and check the result of the C function
via the
object, which is constructed in the test setup.

The above script seems to work; it runs without error, it creates a
file, a
file, and a
directory. I can also
import _magic_tests
without an error.

But I can't figure out how to actually call a C function via CFFI. I can't find any documentation for the
function, and it seems pretty integral to the whole process. The overview mentions it a lot, but the reference contains zero occurrences of it. The docs do have a section on calling functions, but it refers to some
object without showing how it's created. If I look at the previous example there's a
object created from
, but I don't see how to apply that to something that CFFI itself is producing.

My big question (ie. my X problem) is:

  • Is CFFI a reasonable tool to use for calling and testing C functions in a cross platform (Windows 7-10, Linux, OS X) way, and if it is, how?

The questions arising from my current approach (ie. my Y problems) are:

  • Where is the documentation for
    ? How can I find out what arguments it takes?

  • How do I produce
    objects that contain the functions I want to call?

  • Is this the easiest way to use CFFI to call a C function? I do not particularly need or want a shared library or redistributable package to be produced; if it has to happen, that's fine, but it's not necessary. What other approaches could I try?

My current setup is:

  • OS: Windows 10

  • Python: CPython 3.5.1 32 bit

  • Pip: 8.1.2

  • CFFI: 1.6.0

  • C compiler: whatever comes with Visual C++ Build Tools 2015, linked from this MSDN post

I am using CFFI and pycparser from Christoph Gohlke's repository.


For a project of mine, I use cffi to test my C code. IMHO cffi is a great tool to generate python bindings for C code and therefore think that it is a reasonable tool to use for calling and testing C functions from python. However, your code will only be as cross platform as the C code is, since you have to compile the binding for every platform.

Below you can find a few references to the documentation that should answer your questions. Additionally I wrote some example code to illustrate how you would use cffi. For a larger example, you can find my project at

Four your example, would look something like this:

from cffi import FFI

ffibuilder = FFI()

# For every function that you want to have a python binding,
# specify its declaration here
    int add(int a, int b);

# Here go the sources, most likely only includes and additional functions if necessary
    #include "magic.h"
    """, sources=["magic.c"])

if __name__ == "__main__":

To generate the magic_tests module, you have to run python The generated module can be imported and used like this:

from magic_tests import ffi, lib

def run_add():
    assert 4 == lib.add(4, 5)