Skip to content Skip to sidebar Skip to footer

Using Distutils And Build_clib To Build C Library

Does anyone have a good example of using the build_clib command in distutils to build an external (non-python) C library from setup.py? The documentation on the subject seems to be

Solution 1:

Instead of passing a library name as a string, pass a tuple with the sources to compile:

setup.py

import sys
from distutils.core import setup
from distutils.command.build_clib import build_clib
from distutils.extension import Extension
from Cython.Distutils import build_ext

libhello = ('hello', {'sources': ['hello.c']})

ext_modules=[
    Extension("demo", ["demo.pyx"])
]

defmain():
    setup(
        name = 'demo',
        libraries = [libhello],
        cmdclass = {'build_clib': build_clib, 'build_ext': build_ext},
        ext_modules = ext_modules
    )

if __name__ == '__main__':
    main()

hello.c

inthello(void){ return42; }

hello.h

inthello(void);

demo.pyx

cimport demo
cpdef test():
    returnhello()

demo.pxd

cdef externfrom"hello.h":
    inthello()

Code is available as a gist: https://gist.github.com/snorfalorpagus/2346f9a7074b432df959

Post a Comment for "Using Distutils And Build_clib To Build C Library"