用Cython在Windows中封装C++库时的链接器错误

2024-10-02 18:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用cython包装一个c++库。这个库是用visualstudio2008编译的,类似于我的python。我在Windows7上使用Python2.7.9,64位和Cython0.22。在

我的问题是我总是得到一些

LNK2019: unresolved external symbol "...

错误。在

作为一个例子,我用了矩形的例子。我首先创建一个测试库.lib从矩形。h和矩形.cpp然后尝试用cython包装它。这是所有的文件。在

矩形。h

namespace shapes {
class Rectangle {
    public:
        int x0, y0, x1, y1;
        Rectangle(int x0, int y0, int x1, int y1);
        ~Rectangle();
        int getLength();
};
} 

在矩形.cpp在

^{pr2}$

我用visualstudo2008将以上两个文件编译到测试库.lib. 为了包装这个testLib,我使用以下文件:

在pyRectangle.pyx公司在

cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
        Rectangle(int, int, int, int)
        int x0, y0, x1, y1
        int getLength()

cdef class PyRectangle:
    cdef Rectangle *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.thisptr = new Rectangle(x0, y0, x1, y1)
    def __dealloc__(self):
        del self.thisptr
    def getLength(self):
        return self.thisptr.getLength()

在设置.py在

try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

from Cython.Distutils import build_ext

setup(
    name = 'test',
    ext_modules=[Extension("rectangle", 
                            sources = ["pyRectangle.pyx"], 
                             include_dirs=["G:/path/to/cythonTest"],
                             libraries = ["testLib"],
                             library_dirs = ["G:/path/to/cythonTest/testLib/Release"],
                            language="c++",)],
    cmdclass = {'build_ext': build_ext},
)

现在我跑了

Python设置.py构建扩展--就地

得到以下错误

pyRectangle.obj : warning LNK4197: export 'initrectangle' specified multiple times; using first specification

Creating library build\temp.win-amd64-2.7\Release\rectangle.lib and object build\temp.win-amd64-2.7\Release\rectangle.exp

pyRectangle.obj : error LNK2019: unresolved external symbol "public: cdecl shapes::Rectangle::Rectangle(int,int,int,int)" (??0Rectangle@shapes@@QEAA@HHHH@Z) referenced in function "int __cdecl __pyx_pf_9rectangle_11PyRectangle___cinit(struct pyx_obj_9rectangle_PyRectangle *,int,int,int,int)" (?__pyx_pf_9rectangle_11PyRectangle___cinit@@YAHPEAU__pyx_obj_9rectangle_PyRectangle@@HHHH@Z)

pyRectangle.obj : error LNK2019: unresolved external symbol "public: cdecl shapes::Rectangle::~Rectangle(void)" (??1Rectangle@shapes@@QEAA@XZ) referenced in function "void __cdecl __pyx_pf_9rectangle_11PyRectangle_2__dealloc(struct pyx_obj_9rectangle_PyRectangle *)" (?__pyx_pf_9rectangle_11PyRectangle_2__dealloc@@YAXPEAU__pyx_obj_9rectangle_PyRectangle@@@Z)

pyRectangle.obj : error LNK2019: unresolved external symbol "public: int __cdecl shapes::Rectangle::getLength(void)" (?getLength@Rectangle@shapes@@QEAAHXZ) referenced in function "struct _object * __cdecl __pyx_pf_9rectangle_11PyRectangle_4getLength(struct __pyx_obj_9rectangle_PyRectangle *)" (?__pyx_pf_9rectangle_11PyRectangle_4getLength@@YAPEAU_object@@PEAU__pyx_obj_9rectangle_PyRectangle@@@Z)

我认为我的问题与罚单http://trac.cython.org/ticket/106相似,但这在cython版本0.14中被标记为solved,所以实际上应该不再是问题了。在

如果我不首先创建一个库,而是在cython中使用cpp文件本身,我没有问题,但是在实际的大型项目中,我只想简单地包装库。在

有什么好主意吗?在


Tags: fromselfobjcythonintpyx矩形pf