swig没有名为“examp”的模块

2024-06-25 07:07:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法在windows上复制基本SWIG示例。 我的错误已经在SWIG文档中声明了,我确信我已经做了他们提到的两个修复。对于此错误:

>>> 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

威斯康辛州投资委员会的文件明确指出:

忘记前导下划线(\u)。

如果您收到此消息,则表示

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 (_).

我确信我链接到了最新的wrap对象构建,并且我尝试过:“\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

以及build命令:

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

编辑:

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

编辑: 它现在可以工作了,我使用的是外部“C”,不包括.I文件中的头


Tags: theimportyousofooincludeexampletemp
3条回答
<>我发现您必须重命名C++在Windows上生成的.dll到.pID的库文件。我不记得你是否需要在苹果上重新命名它。而要向python公开的函数必须在前面加上外部“C”。否则编译器不会使函数在库外可访问。另外,如果想在python中使用返回值,还需要将其包装为Py_值。

我发现(在windows中),如果创建一个dll,它必须被称为_modulename.pyd 此库(_modulename.pyd)、原始的c++模块myapp.dll和生成的modulename.py必须位于路径以及pythonxx.exe中

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

相关问题 更多 >