* `answer.c` ```c int answer(void) { return 42; } ``` * `answer.h` ```c int answer(void); ``` * `answer.pxd` ```pxd cdef extern from "answer.h": int answer() ``` * `answer.pyx` ```pyx cimport answer cpdef test(): return answer() ``` * `setup.py` ```python 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 libanswer = ('answer', {'sources': ['answer.c']}) ext_modules=[ Extension("demo", ["answer.pyx"]) ] def main(): setup( name = 'answer', libraries = [libanswer], cmdclass = {'build_clib': build_clib, 'build_ext': build_ext}, ext_modules = ext_modules ) if __name__ == '__main__': main() ``` * `test.py` ```python import answer def test_answer(): ret = demo.test() assert(ret == 42) if __name__ == '__main__': test_answer() ``` * Build the C library: ``` $ python setup.py build_clib ``` * Build the extension module in place ``` $ python setup.py build_ext --inplace ```
answer.canswer.hanswer.pxdanswer.pyxcimport answer cpdef test(): return answer()setup.pytest.pyBuild the C library:
Build the extension module in place