python dict ctypes code example
Example: ctypes dictionary
# STEP 1: C++ CODE BELOW:
extern "C" {
int add(int a, int b) {
return a + b;
}
}
# STEP 2: COMPILE
g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl -o libfoo.so foo.o
# STEP 3: PYTHON CODE:
from ctypes import cdll, POINTER, c_int
lib = cdll.LoadLibrary('./libfoo.so')
def call_add(a, b):
assert isinstance(a, int) and isinstance(b, int)
lib.add.restype = c_int
lib.add.argtypes = [c_int,c_int]
return lib.add(c_int(a), c_int(b))
assert call_add(1,4) == 5