Я прочитал соответствующие посты на сайте за целый день. Но похоже, что это общая ошибка, вызванная разными причинами. Так что я все еще публикую свой вопрос здесь. Любые замечания будут оценены!!!
Я пытался обернуть код C ++, который с одним файлом h_feature.h и одним файлом h_feature.cpp только с одним файлом h_f.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
passcdef 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
Задача ещё не решена.
Других решений пока нет …