Suppose I have a library written in Python that I'd like to call from a C software. Is there any way I can compile this Python code to a .so file?
Utilizing the ctypes module to call C/C++ functions in Python. “a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.”
To create a dynamic library in Linux, simply type the following command: gcc *. c -c -fPIC and hit return. This command essentially generates one object file .o for each source file .
This should help.
Also you can do both ways i.e. extend python by writing library in C and call it from Python code OR code functions in C that could be called from Python.
The standard term used (conceptually and help googling is "Python bindings"
Official reference from python docs
General reference - Integrating python with other languages
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With