我试图用cython包装一个c++类,它编译得很好,但是当我导入扩展时,我发现
ImportError: ./svd.so: undefined symbol: _ZTI5model
下面是c++头文件:
首先是"model.h", model是svd的基类。
// model.h
#ifndef MODEL_H_
#define MODEL_H_
#include "common.h"
#include "Data.h"
class model {
protected:
Data data;
public:
model(Data& data);
virtual float predict(uint uid, uint mid);
// evaluate using testset and return final RMSE
virtual float evaluate();
// put predictions to file
void output(string filename);
virtual void onestep();
virtual ~model();
};
#endif /* MODEL_H_ */
则SVD .h, SVD继承自model。
// svd.h
#ifndef SVD_H_
#define SVD_H_
#include "../common.h"
#include "../model.h"
#include "../Data.h"
#define K_NUM 50
namespace SVD{
class svd : model {
public:
svd(Data &data);
void init(uint max_step, float alpha1,
float alpha2, float beta1, float beta2);
float predict(UidType uid, ItemType mid);
float evaluate();
void onestep();
~svd();
}; // end class svd
void initModel(uint max_step, float alpha1, float alpha2, float beta1, float beta2);
};// end namespace svd
#endif /* SVD_H_ */
最后,pyx文件
# distutils: language = c++
# distutils: sources = ../model.cpp ../models/svd.cpp ../common.cpp ../Data.cpp ../model.cpp
cdef extern from "../models/svd.h" namespace "SVD":
cdef cppclass svd:
pass
和setup.py文件
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(
["svd.pyx"], # our Cython source
language="c++", # generate C++ code
))
我认为符号"model"可能是svd的基类,
您必须在基类中为虚方法提供实现,或者(如果该类应该是抽象的)将它们声明为纯方法。例如: