Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setuptools how to build shared library before package installation

Tags:

setuptools

My pacakge has *.py files and *.c files, the *.py files use ctypes to import shared library built from the c source.

Now I have problem how to write my setup.py.

The setup script needs to build my_c_file.c to my_c_file.so, and then copy it to python libpath.

I want to know the what is the 'should' way?

like image 899
hit9 Avatar asked Oct 21 '22 21:10

hit9


1 Answers

You should probably have a look at Building C and C++ Extensions with distutils.

If you build a setup.py file around the example below, setuptools should compile your c file into my_c_lib.so and automatically add it to your installed package (untested).

from distutils.core import setup, Extension

c_module = Extension('my_c_lib',
                sources = ['my_c_file.c'])

setup (name = 'my_package',
    version = '1.0',
    description = 'This is a package in which I compile a C library.',
    ext_modules = [c_module])
like image 116
Joao Cordeiro Avatar answered Oct 23 '22 23:10

Joao Cordeiro