Something wrong when I compile cython with C++

偶尔善良 提交于 2019-12-24 14:34:24

问题


I try to compile the OpenSource Cython Project(https://github.com/seanbell/intrinsic) with gcc. But something is wrong when I use the orde "python setup.py build_ext -i". I have spent several days on it, but it doesn't work and returns the following error when building.

C:\intrinsic-master\bell2014\krahenbuhl2013>python setup.py build_ext -i
Compiling krahenbuhl2013.pyx because it changed.

Cythonizing krahenbuhl2013.pyx

Error compiling Cython file:
------------------------------------------------------------
...
# distutils: language = c++
^
------------------------------------------------------------

krahenbuhl2013.pyx:1:0: 'intrinsic-master.bell2014.krahenbuhl2013.krahenbuhl2013' is not a valid module name
Traceback (most recent call last):
  File "setup.py", line 29, in <module>
    language="c++",
  File "C:\Python27\lib\site-packages\Cython\Build\Dependencies.py", line 753, in cythonize
    cythonize_one(*args[1:])
  File "C:\Python27\lib\site-packages\Cython\Build\Dependencies.py", line 820, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: krahenbuhl2013.pyx

Compiled using the setup.py file below.

import distutils.core
from distutils.extension import Extension
try:
    from Cython.Build import cythonize
    import numpy
except ImportError:
    print "You must have Cython >=0.17 and NumPy to build!"
    import sys
    sys.exit(1)

distutils.core.setup(
    ext_modules=cythonize(Extension(
        'krahenbuhl2013',
        sources=[
            'krahenbuhl2013.pyx',
            "src/densecrf.cpp",
            "src/labelcompatibility.cpp",
            "src/pairwise.cpp",
            "src/permutohedral.cpp",
            "src/unary.cpp",
            "src/util.cpp",
            "src/densecrf_wrapper.cpp",
        ],
        include_dirs=[
            numpy.get_include(),
            "include",
            "/usr/include/eigen3",
        ],
        language="c++",
    )),
)

krahenbuhl2013.pyx is:

# distutils: language = c++
# distutils: sources =src/densecrf_wrapper.cpp


cimport numpy as np

cdef extern from "include/densecrf_wrapper.h":
    cdef cppclass DenseCRFWrapper:
        DenseCRFWrapper(int, int) except +
        void set_unary_energy(float*)
        void add_pairwise_energy(float*, float*, int)
        void map(int, int*)
        int npixels()
        int nlabels()

cdef class DenseCRF:
    cdef DenseCRFWrapper *thisptr

    def __cinit__(self, int npixels, int nlabels):
        self.thisptr = new DenseCRFWrapper(npixels, nlabels)

    def __dealloc__(self):
        del self.thisptr

    def set_unary_energy(self, float[:, ::1] unary_costs):
        if (unary_costs.shape[0] != self.thisptr.npixels() or
                unary_costs.shape[1] != self.thisptr.nlabels()):
            raise ValueError("Invalid unary_costs shape")

        self.thisptr.set_unary_energy(&unary_costs[0, 0])

    def add_pairwise_energy(self, float[:, ::1] pairwise_costs,
                            float[:, ::1] features):
        if (pairwise_costs.shape[0] != self.thisptr.nlabels() or
                pairwise_costs.shape[1] != self.thisptr.nlabels()):
            raise ValueError("Invalid pairwise_costs shape")
        if (features.shape[0] != self.thisptr.npixels()):
            raise ValueError("Invalid features shape")

        self.thisptr.add_pairwise_energy(
            &pairwise_costs[0, 0],
            &features[0, 0],
            features.shape[1]
        )

    def map(self, int n_iters=10):
        import numpy as np
        labels = np.empty(self.thisptr.npixels(), dtype=np.int32)
        cdef int[::1] labels_view = labels
        self.thisptr.map(n_iters, &labels_view[0])
        return labels

Any help would be greatly appreciated!

Thanks, Limumu.

来源:https://stackoverflow.com/questions/32799506/something-wrong-when-i-compile-cython-with-c

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