Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Python packaging for packages containing C libs #123

Open
veit opened this issue Jul 1, 2021 · 0 comments
Open

Update Python packaging for packages containing C libs #123

veit opened this issue Jul 1, 2021 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@veit
Copy link
Owner

veit commented Jul 1, 2021

  • 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
    
@veit veit self-assigned this Jul 1, 2021
@veit veit added documentation Improvements or additions to documentation enhancement New feature or request labels Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant