Skip to content

Instantly share code, notes, and snippets.

@snorfalorpagus
Created January 25, 2016 10:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save snorfalorpagus/2346f9a7074b432df959 to your computer and use it in GitHub Desktop.
Save snorfalorpagus/2346f9a7074b432df959 to your computer and use it in GitHub Desktop.
Using distutils and build_clib to build a C library

Using distutils and build_clib to build a C library

This repository demonstrates how to build a C library that is included in the module distribution.

To build the C library:

python setup.py build_clib

To build the extension module (in place):

python setup.py build_ext --inplace

To test the module:

python test.py
# or
py.test test.py
cdef extern from "hello.h":
int hello()
cimport demo
cpdef test():
return hello()
int hello(void) { return 42; }
int hello(void);
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"])
]
def main():
setup(
name = 'demo',
libraries = [libhello],
cmdclass = {'build_clib': build_clib, 'build_ext': build_ext},
ext_modules = ext_modules
)
if __name__ == '__main__':
main()
import demo
def test_hello():
ret = demo.test()
assert(ret == 42)
if __name__ == '__main__':
test_hello()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment