lilei123
lilei123

Reputation: 175

Problems while creating a c++ extension with cython

I'm working on osx 10.8.4 64 bit with python 2.7, cython 0.19.1 and numpy 1.6.1.

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

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!

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;
    }
}

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

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
  ))

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!

Upvotes: 4

Views: 965

Answers (1)

Brutos
Brutos

Reputation: 701

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.

Upvotes: 1

Related Questions