遵循Web上的不同教程,我尝试使用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行)。
知道我该怎么做才能纠正这个问题?
答案 0 :(得分:4)
在SWIG文档中,paragraph 31.2.2声明库的名称.so应为_NameOfTheModule.so
所以我重命名了我的库_LibraryInstance.so
,而不是LibraryInstance.so
...现在我的模块加载正常。