问题描述
我正在使用python 2.7,cython 0.19.1和numpy 1.6.1。开发osx 10.8.4 64位。
I'm working on osx 10.8.4 64 bit with python 2.7, cython 0.19.1 and numpy 1.6.1.
我正在尝试创建一个与python一起使用的c ++扩展。给出了c ++代码,我编写了一个包装c ++类,使使用python中所需的函数更容易。编译可以工作,但是导入扩展文件会导致以下错误:
I'm trying to create an c++ extension to be used with python. The c++ code is given and I wrote a wrapper c++ class to make using the needed functions in python easier.Compiling works but importing the extension file causes the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(./mserP.so, 2): Symbol not found: __ZN4mser12MSERDetectorC1Ejj
Referenced from: ./mserP.so
Expected in: flat namespace
in ./mserP.so
我用一个简单的c ++类尝试了一个较小的示例,该类的函数具有一个numpy数组作为其参数。
I tried a smaller example with an easy c++ class with a function that has got a numpy array as its argument. Importing and using of the extension file works great!
这里是包装器类(maser_wrapper.cpp):
Here the wrapper class (maser_wrapper.cpp):
#include "mser_wrapper.h"
#include "mser.h"
#include <iostream>
namespace mser {
CallMser::CallMser(unsigned int imageSizeX,unsigned int imageSizeY)
{
//Create MSERDetector
mser::MSERDetector* detector = new mser::MSERDetector(imageSizeX, imageSizeY);
}
CallMser::~CallMser()
{
delete detector;
}
}
这是cython文件(mserP.pyx):
And here the cython file (mserP.pyx):
# distutils: language = c++
# distutils: sources= mser_wrapper.cpp
cdef extern from "mser_wrapper.h" namespace "mser":
cdef cppclass CallMser:
CallMser(unsigned int, unsigned int) except +
cdef class PyCallMser:
cdef CallMser *thisptr
def __cinit__(self, unsigned int imageSizeX, unsigned int imageSizeY):
self.thisptr = new CallMser(imageSizeX, imageSizeY)
def __dealloc__(self):
del self.thisptr
最后但并非最不重要的setup.py:
Last but not least the setup.py:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(
"mserP.pyx", # our Cython source
sources=["mser_wrapper.cpp"], # additional source file(s)
language="c++", # generate C++ code
))
在名称空间 mser中,存在 MSERDetector类,但不能找到了。它是在我的包装器类中包含的头文件 mser.h中定义的。
In namespace "mser" the class "MSERDetector" exists but cannot be found. It's defined in the header file "mser.h" which is included by my wrapper class.
有人知道这个问题可能是什么吗?谢谢!
Has anybody an idea what the problem could be? Thanks!
推荐答案
您缺少mser.cpp中的对象代码。告诉cython将其添加到cython文件中的setup.py和distutil源代码中,以包括它。
You are missing the object code from mser.cpp. Tell cython to include it by adding it to the sources in setup.py and distutil sources in the cython file.
这篇关于使用cython创建C ++扩展时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!