This is a simple demonstration of using ctypes to call C code from python especially to process numpy array type opjects.
- Compile C sources to shared library
$ gcc -shared -fpic -o libtest.so test.c- Run the main python file main.py.
$ python main.pyThe C function is implemented in test.c. It takes a int64 vector as input and processes an output vector of the same type.
The test.c file is compiled into a shared libary by
$ gcc -shared -fpic -o libtest.so test.cTo access the libary we have to load it with ctypes. For convenient usage and type checking I recommend to write a small Python wrapper function and specify the input interface, like in test.py.
Now the library can be called in a native way like shown in main.py:
ctypes works on the ABI level and can only be used for C code, not C++. If you want to interface C++ you either need to write a C wrapper or use a differnt wrapping tool.