Skip to content

Update Python packaging for packages containing C libs #123

@veit

Description

@veit
  • answer.c

    int answer(void) { return 42; }
  • answer.h

    int answer(void);
  • answer.pxd

    cdef extern from "answer.h":
        int answer()
  • answer.pyx

    cimport answer
    cpdef test():
        return answer()
  • 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
    
    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

    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
    

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions