Let`s discuss the problem of accessing C code from Python. Because it is quite obvious that many of the built-in Python libraries are written in C. Thus, access to C is a very important part of Python`s interoperability with existing libraries. Python provides a rich API for programming in C, but there are many different ways to work with C.
Code # 1: [
work.c ] The C code we`re dealing with.
The above code has various C programming features.
divide () - returning multiple values, one through a pointer argument
avg () - performing a data reduction across a C array
Point and distance () - involve C structures.
Let`s assume the above code is in a file named work.c and compiled into a library that can be linked to other C code. We now have a number of C functions that have been compiled into a shared library. So, we call functions entirely from Python without the need to write additional C code or use a third-party extension tool.
Will play in Python ctypes , but make sure the C code to be converted has been compiled into a shared library that is compatible with the Python interpreter (e.g. same architecture, word size, compiler, etc.).
Also, the libsample.so file has been placed in the same directory as
work.py Let`s get to the
Code # 2: Python module that wraps the resulting library to access it
Code # 3: Code Access