Skip to content
/ numlib Public

A Simple wrapper to create a simple extension module for python3 from C

License

Notifications You must be signed in to change notification settings

ms3c/numlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

A Simple wrapper to create a simple extension module for python3 from C

Resources

License

Stars

Watchers

Forks

Packages

No packages published