from cffi import FFI ffi = FFI() ffi.cdef(""" int add(int a, int b); """) math_lib = ffi.dlopen("./math.so") result = math_lib.add(3, 5) print(result)
from cffi import FFI ffi = FFI() ffi.cdef(""" typedef struct { int x; int y; } Point; """) lib = ffi.dlopen("./geometry.so") point = lib.create_point(2, 4) x = lib.get_x(point) y = lib.get_y(point) print("x: {}, y: {}".format(x, y))In this example, we define a C struct 'Point' using the typeof function. We then load a shared library (geometry.so) that includes functions to create a Point struct, get the x coordinate, and get the y coordinate. We create a Point instance and then retrieve its x and y values using the C functions. Overall, the CFFI package library allows us to work with C code within our Python programs, which can be especially useful for performance-critical tasks or when working with legacy codebases.