swig 没有名为 _example 的模块

发布于 2024-10-01 06:24:48 字数 1662 浏览 5 评论 0原文

我无法在 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文档明确指出:

忘记前导下划线(_)。

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

你要么忘记编译 将代码包装到扩展模块中 或者你没有给出延期 模块的正确名称。确保 你将包装器编译成 名为 example.so 的模块。并且不要 忘记前导下划线 ()。忘记前导下划线 (_)。

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

编辑:

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

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

I cannot reproduce the basic SWIG example on windows.
My error is stated in the SWIG docs and I am sure that I do the 2 fixes they mention. For this error:

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

the SWIG documentation clearly states:

forget the leading underscore (_).

forget the leading underscore (_).> If you get this message, it means that

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

and I am sure that I link with the latest wrap object build and I have tryied: "_example", "_example.so", "example.dll", "example.so", "example.dll", even all at once, and that the generated "example.py" is in the same folder as the shared library, and that the python path contains this directoryforget the leading underscore ().

THE EXAMPLE:

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

and the build commands:

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

even if I don't use -O3 it still doesn't work (I pasted the build commands from a Release configuration)

I also tried this and to no success:

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

EDIT:

apparently it loads the dll if you rename it to "_example.pyd", BUT the module loaded does not contain my "foo_sum" function

EDIT:
it works now, I am using extern "C" and not including headers in the .i file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无尽的现实 2024-10-08 06:24:48

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

The file name of the library must be *.pyd. I suppose that you have generated a wrapper code and link it together.

顾挽 2024-10-08 06:24:48

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

I found that (in windows), if you make a dll it has to be called _modulename.pyd
This library (_modulename.pyd), the original c++ module myapp.dll and the resulting modulename.py have to be in the path as well as pythonxx.exe

梦忆晨望 2024-10-08 06:24:48

我发现你必须在 Windows 上将 C++ 生成的库文件从 .dll 重命名为 .pyd。我不记得你是否需要在苹果上重命名它。并且你想要暴露给 python 的函数必须在其前面有 extern "C" 。否则,编译器不会使该函数可以在库外部访问。另外,如果我记得如果你想在 python 中使用它们,你需要将返回值包装在 Py_value 中。

I found you have to rename the library file that C++ generates from .dll to .pyd on windows. I can't recall if you need to rename it on apple. and your function that you want to expose to python has to have extern "C" preceding it. Otherwise the compiler doesn't make the function accessible outside the library. Also If I recall you need to wrap the return values in a Py_value if you want to use them in python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文