This is a simple tutorial on how to write python extension in C through a process called "wrapping" or creating a Python extension module.
Use the package manager pip to install python-config and apt for installing python3-dev.
pip install python-config
sudo apt-get install python3-dev
To compile this you will need the GNU C compiler Collection gcc
sudo apt-get install build-essential gcc
gcc add.c $(python3-config --includes) -shared -fPIC -o addnums.so
OR
gcc -o addnums.so -shared -fPIC add.c -I/path/to/python/include -lpython3.x
import addnums # make sure you are in same directory as addnums.so
result = addnums.add_numbers(5, 4)
print(result) # Output: 9
addnums/
├── addnums.so
├── add.c
├── add.py
└── setup.py
from setuptools import setup, Extension
setup(
name='addnums',
version='1.0',
ext_modules=[Extension('addnums', ['addnums.so'])],
packages=['addnums'],
license='GPL',
long_description=open('README.md').read(),
)
python3 setup.py bdist_wheel