按照网络上的不同教程,我尝试使用 SWIG 在 python 中制作一个 c++ 类的包装器。
我的类(class)是这样的:
/*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 文件。
当我查看 swig 生成的 LibraryInstance.py
时,我可以清楚地看到 class LibraryInstance
:
cf. entire generated python wrapper here.
但是当我在与我的 .so 相同的目录中启动命令 python LibraryInstance.py
时,我看到了这个错误输出:
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 行)。
知道我应该怎么做才能纠正这个问题吗?
最佳答案
在 SWIG 文档中,paragraph 31.2.2说明库.so的名称应该是_NameOfTheModule.so
所以我重命名了我的库 _LibraryInstance.so
,而不是 LibraryInstance.so
...现在我的模块加载正常。
关于c++ - Python 提示 SWIG 模块不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14609637/