我无法在 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/