【发布时间】:2013-01-30 17:06:06
【问题描述】:
按照网络上的不同教程,我尝试使用 SWIG 在 python 中制作 c++ 类的包装器。
我的班级是这样的:
/*file libraryInstance.h*/
struct LibraryInstance
{
void init();
void terminate();
private:
std::shared_ptr<AnObject> m_spAnObject;
};
为了展示 python,我制作了这个 .i 文件:
%module LibraryInstance
%{
#include "libraryInstance.h"
%}
%include "libraryInstance.h"
那么我已经执行了命令swig -c++ -python -o ./src/libraryInstance_wrap.cpp ./src/libraryInstance.i
没有任何输出错误,swig 生成了两个文件,libraryInstance_wrap.cpp 和LibraryInstance.py
然后我编译 c++ 文件,包括libraryInstance_wrap.cpp。所有编译都很好,我得到了我的库 .so 文件。
当我查看生成的LibraryInstance.py swig 时,我可以清楚地看到class LibraryInstance:
cf. entire generated python wrapper here.
但是当我启动命令 python LibraryInstance.py 时,在与我的 .so 相同的目录中,我看到了这个错误输出:
Traceback (most recent call last):
File "LibraryInstance.py", line 26, in <module>
_LibraryInstance = swig_import_helper()
File "LibraryInstance.py", line 18, in swig_import_helper
import _LibraryInstance
ImportError: No module named _LibraryInstance
当我查看LibraryInstance.py的代码时,它看起来好像抛出了异常ImportError,python找不到模块。 (第 18 行)。
知道我应该怎么做才能纠正这个问题吗?
【问题讨论】: