c++ - Python 提示 SWIG 模块不存在

标签 c++ python linux swig

按照网络上的不同教程,我尝试使用 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.cppLibraryInstance.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/

相关文章:

c++ to_string was not declared in this scope错误【Windows+Devcpp环境】

c++ - 数组下标符号和解引用

python - 如何递归地增加列表中的第一个值并附加到 python 中的嵌套列表?

linux - shell 脚本内的 perl -p -i -e

linux - 在 unix 中备份特定限制的文件

c++ - 如何从 C++ 调用 javascript 回调

c++ - 用于模拟运行时数字模板参数的成语?

python - AJAX GET 请求成功; Django View 已更新但 HTML 未更新

python - 维护段落计数

linux - 在服务器端 Swift 上计算表达式