Calling C++ code from Python using Cython whith the distutilis approach

我们两清 提交于 2019-12-03 03:56:39
marcelosalloum

Resolved, thanks to Dietmar Kühl's comments and this video from youtube!

What was wrong? I found out that my setup.py was misconfigured. It should be like:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    name = 'DyCppInterface',
    version = '1.0',
    author = 'Marcelo Salloum dos Santos',
    # The ext modules interface the cpp code with the python one:
    ext_modules=[
        Extension("rectangle",
            sources=["rectangle.pyx", "cpp_rect.cpp"], # Note, you can link against a c++ library instead of including the source
            include_dirs=[".","source" , "/opt/local/include/opencv", "/opt/local/include"],
            language="c++",
            library_dirs=['/opt/local/lib', 'source'],
            libraries=['opencv_core', 'LibCppOpenCV'])
    ],
    cmdclass = {'build_ext': build_ext},
)

The three things to pay attention in order to correctly configure it are:

  • include_dirs: each referenced file in the setup.py or the .h and .cpp shall have its container folder in the include_dirs;
  • library_dirs: each referenced library shall have its container folder written here;
  • libraries: one MUST put the library's name here

Further questions on how to configure a library for cython can be answered by watching this video on how to use and configure a dynamic library (using Eclipse CDT).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!