Python Cython importorror:./h\u f.so:未定义符号:_zn9haystack27特征1ev由操作引起,仅允许在c++;来自新功能()的错误

Python Cython importorror:./h\u f.so:未定义符号:_zn9haystack27特征1ev由操作引起,仅允许在c++;来自新功能()的错误,python,c++,cython,importerror,Python,C++,Cython,Importerror,我在网站上读了一整天的相关帖子。但这似乎是由不同原因造成的一般性错误。所以我还是把我的问题贴在这里。任何意见将不胜感激 我试图包装一个具有一个HyFask.h和一个HyFasual.CPP文件的C++代码,只有一个Hyf.Pyx文件。下面是我的h_f.pyx文件: from libcpp cimport bool cdef extern from "h_feature.h" namespace "h2": cdef cppclass feature: feature()

我在网站上读了一整天的相关帖子。但这似乎是由不同原因造成的一般性错误。所以我还是把我的问题贴在这里。任何意见将不胜感激

<>我试图包装一个具有一个HyFask.h和一个HyFasual.CPP文件的C++代码,只有一个Hyf.Pyx文件。下面是我的h_f.pyx文件:

from libcpp cimport bool
cdef extern from "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:     # this is a struct in .h file, but i write it as 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
下面是我的setup.py:

    from distutils.core import setup, Extension
    from Cython.Build import cythonize

    compile_args = ['-g', '-std=c++11']
    setup(ext_modules = cythonize(Extension(
       "h_f",                                # the extension name
       sources=["h_f.pyx", "h_feature.cpp"], # the Cython source and
       extra_compile_args=compile_args,               
       language="c++",                        # generate and compile C++ code
  )))
以下是编译过程中的警告:

    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' is 
    valid for C/ObjC but not for C++ [enabled by default]
    In file included from /usr/include/python2.7/Python.h:121:0,
             from 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 from string constant to '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' is 
    valid for C/ObjC but not for C++ [enabled by default]
    In file included from /usr/include/python2.7/Python.h:121:0,
             from 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 from string constant to '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
根据评论,我在下面添加了我的c_feature.h:

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 to 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如下:

namespace h2 {

    typedef std::mutex Mutex;

    // use the Lock primitive to acquire a given mutex for the object's lifetime. 
    // the mutex is released when the object is destroyed, typically when it 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
    };

    // for now, use a heavyweight mutex to prevent concurrency issues in the API.
    // that is, all API calls must acquire the mutex and hold it for the entire call.
   static Mutex threadMutex;


  class feature_impl
  {
    // the base class for feature. threadMutex is used in this class. ommitted all implementation of this 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

重要的细节可能在h_feature.cpp中,而没有看到您的h_feature.h和h_feature.cpp。我敢打赌,您在h文件中声明析构函数,而不是在cpp文件中实现它。我添加了我的.h文件并解释了.cpp文件。请你再亮一点好吗?谢谢~!请也添加cpp文件,没有它是不可能告诉。请随意最小化添加的examplehi cpp文件。让我知道这是否正常~重要的细节可能在h_feature.cpp中,而没有看到您的h_feature.h和h_feature.cpp。我打赌您在h文件中声明析构函数,而不是在cpp文件中实现它。我添加了我的.h文件并解释了.cpp文件。请你再亮一点好吗?谢谢~!请也添加cpp文件,没有它是不可能告诉。请随意最小化添加的examplehi cpp文件。如果可以,请告诉我~