from cffi import FFI import ctypes ffi = FFI() # Load the shared library at runtime using dlopen lib = ctypes.CDLL("libmylib.so") # Define C functions we want to call ffi.cdef(""" void say_hello(); int add(int a, int b); """) # Call a C function lib.say_hello() # Call another C function result = lib.add(2, 3) print(result)
from cffi import FFI import ctypes ffi = FFI() # Load the shared library at runtime using dlopen with a specific symbolic name lib = ctypes.CDLL(None, mode=ctypes.RTLD_GLOBAL) # Load the library with a specific name. In this example "mylib.so" is loaded lib.dlopen(b"mylib.so", ctypes.RTLD_LAZY)This example loads a shared library with a specific symbolic name ("mylib.so") using cffi's dlopen. It first loads libc using ctypes.CDLL with mode=ctypes.RTLD_GLOBAL. Then loads shared library with a specific name using lib.dlopen. Package library: This example uses ctypes package which is part of the Python standard library.