用C ++编译cython时出错了

时间:2015-09-26 16:44:52

标签: python c++ gcc cython

我尝试使用gcc编译OpenSource Cython项目(https://github.com/seanbell/intrinsic)。但是当我使用orde“python setup.py build_ext -i”时出了点问题。我花了几天时间,但它不起作用,并在构建时返回以下错误。

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

使用下面的setup.py文件编译。

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是:

# 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

非常感谢任何帮助!

谢谢,Limumu。

0 个答案:

没有答案