Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ms3c/numlib

Repository files navigation

addnums

This is a simple tutorial on how to write python extension in C through a process called "wrapping" or creating a Python extension module.

Installation

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

Compiling the shared library

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

Usage

import addnums # make sure you are in same directory as addnums.so
result = addnums.add_numbers(5, 4)
print(result) # Output: 9

Distributing the package

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

About

An example to show how to create a simple Python extension module in C

Resources

License

Stars

Watchers

Forks

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /