c++ - swig 没有名为 _example 的模块

标签 c++ python gcc swig

我无法在 Windows 上重现基本的 SWIG 示例。 我的错误在 SWIG 文档中有说明,我确信我做了他们提到的 2 个修复。对于这个错误:

>>> import example
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "example.py", line 2, in ?
    import _example
ImportError: No module named _example

SWIG 文档明确指出:

忘记前导下划线 (_)。

忘记前导下划线 (_)。>如果您收到此消息,则表示

you either forgot to compile the wrapper code into an extension module or you didn't give the extension module the right name. Make sure that you compiled the wrappers into a module called example.so. And don't forget the leading underscore ().forget the leading underscore (_).

我确信我链接了最新的包装对象构建并且我已经尝试过:“_example”、“_example.so”、“example.dll”、“example.so”、“example.dll” “,甚至一次全部生成,并且生成的“example.py”与共享库位于同一文件夹中,并且 python 路径包含此目录忘记前导下划线 ()。

示例:

//example.h
int foo_sum(int a, int b);

.

//example.cpp
int foo_sum(int a, int b) {
    return a + b;
}

.

//example.i
%module example
%{
#include "example.h"
%}

#include "example.h

和构建命令:

gcc -IV:\temp\example\external\include\Python -O3 -Wall -c -fmessage-length=0 -oexample_wrap.o ..\example_wrap.c
g++ -IV:\temp\example\external\include\Python -O3 -Wall -c -fmessage-length=0 -oexample.o ..\example.cpp
g++ -LV:\temp\example\external\lib -shared -oexample.dll example_wrap.o example.o -lpython26

即使我不使用 -O3 它仍然不起作用(我从发布配置粘贴构建命令)

我也试过这个但没有成功:

>>> import sys
>>> sys.path.append("/your/module/path")
>>> import example

编辑:

显然,如果您将它重命名为“_example.pyd”,它会加载 dll,但加载的模块不包含我的“foo_sum”函数

编辑: 它现在可以工作了,我正在使用 extern "C"并且不包括 .i 文件中的标题

最佳答案

库的文件名必须是*.pyd。我想您已经生成了包装代码并将其链接在一起。

关于c++ - swig 没有名为 _example 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4095083/

相关文章:

c++ - 我可以访问唯一指针作为 const 引用吗?

c++ - 用其他基类的方法来满足纯抽象方法的最简单方法是什么

python - Matlab pyversion 命令找不到 python3.4 的库

python - 永远不会创建文件

编译.C 文件 : Undefined symbols for architecture x86_64

c++ - 使用 C++ 模板作为 objective-c 方法的参数

python - 过滤前将函数应用于列

c - 在系统 verilog 代码中集成 fftw C 函数调用

c - 汇编代码 : logic behind calculating offset into stack

c++ - 当返回非 void 的函数中没有 return 语句时检测到堆栈粉碎