Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.rst

Example

This is a simple demonstration of using ctypes to call C code from python especially to process numpy array type opjects.

Usage

  1. Compile C sources to shared library
$ gcc -shared -fpic -o libtest.so test.c
  1. Run the main python file main.py.
$ python main.py

Explanation

The 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.c

To 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.