Wrap C++ lib with Cython

前端 未结 3 876
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 12:37

I\'m new to Cython and I\'m trying to use Cython to wrap a C/C++ static library. I made a simple example as follow.

Test.h:

#ifndef          


        
3条回答
  •  再見小時候
    2020-12-23 13:12

    If your C++ code is only used by the wrapper, another option is to let the setup compile your .cpp file, like this:

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext
    
    ext_modules = [Extension("test",
                         ["test.pyx", "test.cpp"],
                         language='c++',
                         )]
    
    setup(
      name = 'test',
      cmdclass = {'build_ext': build_ext},
      ext_modules = ext_modules
    )
    

    For linking to a static library you have to use the extra_objects argument in your Extension:

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext
    
    ext_modules = [Extension("test",
                         ["test.pyx"],
                         language='c++',
                         extra_objects=["libtest.a"],
                         )]
    
    setup(
      name = 'test',
      cmdclass = {'build_ext': build_ext},
      ext_modules = ext_modules
    )
    

提交回复
热议问题