python - Cython ImportError: ./h_f.so: undefined symbol: _ZN9haystack27featureD1Ev caused by Operation only allowed in c++ error from new feature() -
i have read relevant posts on site whole day. seems general error caused different reasons. still post question here. comments appreciated!!!
i trying wrap c++ code 1 h_feature.h , 1 h_feature.cpp file 1 h_f.pyx file. below h_f.pyx file:
from libcpp cimport bool cdef extern "h_feature.h" namespace "h2": cdef cppclass feature: feature() except + feature(char* modeldir, bool) except + bool load_feature_extractor(char* modeldir, bool) cdef cppclass image: # struct in .h file, write class in pyx file here pass cdef class pyimage: cdef image* image cdef class pyfeature: cdef feature* c_feature def __cinit__(self, bytes modeldir, bint usegpu = 0): self.c_feature = new feature(modeldir, usegpu) def pyload_feature_extractor(self, bytes modeldir, bint usegpu = 0): return self.c_feature.load_feature_extractor(modeldir, usegpu) def __dealloc__(self): del self.c_feature
below setup.py:
distutils.core import setup, extension cython.build import cythonize compile_args = ['-g', '-std=c++11'] setup(ext_modules = cythonize(extension( "h_f", # extension name sources=["h_f.pyx", "h_feature.cpp"], # cython source , extra_compile_args=compile_args, language="c++", # generate , compile c++ code )))
below warning during compile:
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -dndebug -g - fwrapv -o2 -wall -wstrict-prototypes -fpic -i/usr/include/python2.7 -c h_feature.cpp -o build/temp.linux-x86_64-2.7/h_feature.o cc1plus: warning: command line option '-wstrict-prototypes' valid c/objc not c++ [enabled default] in file included /usr/include/python2.7/python.h:121:0, h_feature.cpp:21: hashtag2.cpp: in function 'pyobject* newarrayobject(pytypeobject*, py_ssize_t, arraydescr*)': /usr/include/python2.7/pyerrors.h:220:74: warning: deprecated conversion string constant 'char*' [-wwrite-strings] #define pyerr_badinternalcall() _pyerr_badinternalcall(__file__, __line__) ^ h_feature.cpp:959:9: note: in expansion of macro 'pyerr_badinternalcall' pyerr_badinternalcall(); ^ x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -dndebug -g - fwrapv -o2 -wall -wstrict-prototypes -fpic -i/usr/include/python2.7 -c h_feature.cpp -o build/temp.linux-x86_64- 2.7/h_feature.o cc1plus: warning: command line option '-wstrict-prototypes' valid c/objc not c++ [enabled default] in file included /usr/include/python2.7/python.h:121:0, h_feature.cpp:21: h_feature.cpp: in function 'pyobject* newarrayobject(pytypeobject*, py_ssize_t, arraydescr*)': /usr/include/python2.7/pyerrors.h:220:74: warning: deprecated conversion string constant 'char*' [-wwrite-strings] #define pyerr_badinternalcall() _pyerr_badinternalcall(__file__, __line__) ^ h_feature.cpp:959:9: note: in expansion of macro 'pyerr_badinternalcall' pyerr_badinternalcall(); ^ c++ -pthread -shared -wl,-o1 -wl,-bsymbolic-functions -wl,- bsymbolic-functions -wl,-z,relro -fno-strict-aliasing -dndebug -g - fwrapv -o2 -wall -wstrict-prototypes -d_fortify_source=2 -g - fstack-protector --param=ssp-buffer-size=4 -wformat -werror=format- security build/temp.linux-x86_64-2.7/hashtag2.o build/temp.linux- x86_64-2.7/haystack2_feature.o -o /root/h_feature/project/src/feature_wrapper/h_feature.so
based on comment, add c_feature.h below:
namespace h2 { typedef unsigned char pixel_type; struct image; class feature_impl; class dllexport feature { public: feature(); feature(const char* modeldir, bool usegpu = false); ~feature(); /*------------------------------------------------------- method load feature extractor input: modeldir model directory (...model/) */ bool load_feature_extractor(const char* modeldir, bool usegpu = false); } struct image { image() { } image(const image& i) : width(i.width), height(i.height), bytes_per_row(i.bytes_per_row), pixels(i.pixels) { } image(pixel_type* p, int w, int h, int bpr) : width(w), height(h), bytes_per_row(bpr), pixels(p) { } int width,height,bytes_per_row; pixel_type* pixels; }; }
h_feature.cpp below:
namespace h2 { typedef std::mutex mutex; // use lock primitive acquire given mutex object's lifetime. // mutex released when object destroyed, typically when goes // out of scope. class lock { public: lock(mutex& _m) : m(_m) { m.lock(); } ~lock() { m.unlock(); } private: mutex& m; // don't allow copying or null constructor lock(); // unimplemented lock(const lock&); // unimplemented lock& operator=(const lock&); // unimplemented }; // now, use heavyweight mutex prevent concurrency issues in api. // is, api calls must acquire mutex , hold entire call. static mutex threadmutex; class feature_impl { // base class feature. threadmutex used in class. ommitted implementation of class. } feature::feature() : fi(new feature_impl) { } feature::feature(const char* modeldir, bool usegpu) : fi(new feature_impl) { fi->load_feat_extractor(modeldir, usegpu); } feature::~feature() { if(fi != null) delete fi; } bool feature::load_feature_extractor(const char* modeldir, bool usegpu) { return fi->load_feat_extractor(std::string(modeldir), usegpu); } }; // namespace h2
Comments
Post a Comment