from cffi import FFI ffi = FFI() ffi.cdef(""" int add(int a, int b); """) libexample = ffi.dlopen("libexample.so") print(ffi.emit_c_code("add_wrapper", """ int add_wrapper(int a, int b) { return add(a, b); } """))
typedef struct { char *name; int age; } Person;
from cffi import FFI ffi = FFI() ffi.cdef(""" typedef struct { char *name; int age; } Person; """) print(ffi.emit_c_code("Person", """ typedef struct { char *name; int age; } Person; """))The above code generates a C struct called Person that can be used in C code. In both examples above, the package library is libexample, which is represented by the ffi.dlopen() call.