加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用cython创建c扩展时出现问题

发布时间:2020-12-16 07:05:24 所属栏目:百科 来源:网络整理
导读:我正在使用 python 2.7,cython 0.19.1和numpy 1.6.1开发osx 10.8.4 64位. 我正在尝试创建一个与python一起使用的c扩展.给出了c代码,我编写了一个包装器c类,以便在python中使用所需的函数.编译工作但导入扩展文件会导致以下错误: Traceback (most recent cal
我正在使用 python 2.7,cython 0.19.1和numpy 1.6.1开发osx 10.8.4 64位.

我正在尝试创建一个与python一起使用的c扩展.给出了c代码,我编写了一个包装器c类,以便在python中使用所需的函数.编译工作但导入扩展文件会导致以下错误:

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数组作为参数.导入和使用扩展文件非常有用!

这里的包装类(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):

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

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”中定义,它包含在我的包装器类中.

有谁知道问题可能是什么?谢谢!

解决方法

您缺少mser.cpp中的对象代码.告诉cython将它添加到cython文件中的setup.py和distutil源代码中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读