简体   繁体   English

绑定 C++ 和 Python 时,SWIG 在具有 MinGW-w64 的 Windows 上不起作用:导入时 DLL 加载失败:找不到指定的模块

[英]SWIG doesn't work on Windows with MinGW-w64 when binding C++ and Python: DLL load failed while importing: The specified module could not be found

I am trying to bind C++ with Python on Windows using SWIG and MinGW-w64 g++.我正在尝试使用 SWIG 和 MinGW-w64 g++ 在 Windows 上将 C++ 与 Python 绑定。 So far I got a factorial calculator function in C++:到目前为止,我在 C++ 中得到了一个阶乘计算器函数:

// factorial.cpp
long fact(long num)
{
    if (num <= 1) return 1;
    return num * fact(num - 1);
}

This is my factorial.i:这是我的 factorial.i:

%module factorial
%{
extern long fact(long num);
%}

extern long fact(long num);

I then use Command Prompt to build these:然后我使用命令提示符来构建这些:

swig -python -c++ factorial.i

This generates factorial_wrap.cxx and factorial.py.这将生成 factorial_wrap.cxx 和 factorial.py。 I then use MinGW-w64 g++ to build these:然后我使用 MinGW-w64 g++ 来构建这些:

g++ -c factorial.cpp
g++ -c -fpic factorial_wrap.cxx -IC:\Users\kritomas\AppData\Local\Programs\Python\Python38\include
g++ factorial.o factorial_wrap.o -LC:\Users\kritomas\AppData\Local\Programs\Python\Python38\libs -shared -lpython38 -o _factorial.pyd

So far no problem.到目前为止没有问题。 The problem occurs when I then open up Python in Command Prompt and try to import these:当我然后在命令提示符中打开 Python 并尝试导入这些时,就会出现问题:

C:\Users\kritomas\Desktop>python
Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import factorial
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\kritomas\Desktop\factorial.py", line 15, in <module>
    import _factorial
ImportError: DLL load failed while importing _factorial: The specified module could not be found.
>>>

Here's the error, as you can see above.这是错误,正如您在上面看到的那样。 I already tried Dependency Walker, that gave me 2 missing DLLs.我已经尝试过 Dependency Walker,它给了我 2 个缺失的 DLL。 After putting both of them into System32, it still happens.把这两个都放进System32后,还是这样。 I already tried using extern "C" instead of extern:我已经尝试使用 extern "C" 而不是 extern:

%module factorial
%{
extern "C" long fact(long num);
%}

extern "C" long fact(long num);

It just straight up removes fact():它直接删除了 fact():

C:\Users\kritomas\Desktop>g++ factorial.o factorial_wrap.o -LC:\Users\kritomas\AppData\Local\Programs\Python\Python38\libs -shared -lpython38 -o _factorial.pyd
factorial_wrap.o:factorial_wrap.cxx:(.text+0x38d7): undefined reference to `fact'
collect2.exe: error: ld returned 1 exit status

I have the same issue with Python 3.9.7, the only reason why I downgraded to 3.8.10 is because it runs perfectly on my Linux Mint VM (which has Python 3.8.10), if instead of running:我对 Python 3.9.7 有同样的问题,我降级到 3.8.10 的唯一原因是因为它在我的 Linux Mint VM(具有 Python 3.8.10)上完美运行,如果不是运行:

g++ -c -fpic factorial_wrap.cxx -IC:\Users\kritomas\AppData\Local\Programs\Python\Python38\include
g++ factorial.o factorial_wrap.o -LC:\Users\kritomas\AppData\Local\Programs\Python\Python38\libs -shared -lpython38 -o _factorial.pyd

I run:我跑:

g++ -c -fpic factorial_wrap.cxx -I/usr/include/python3.8
g++ factorial.o factorial_wrap.o -shared -o _factorial.so

It works perfectly:它完美地工作:

kritomas@kritomas-virtual-machine:~/Desktop$ python
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import factorial
>>> factorial.fact(5)
120

What am I doing wrong?我究竟做错了什么? Is there anything I am missing here?有什么我在这里想念的吗? Please help me.请帮我。 Thanks in advance ;)提前致谢 ;)

I met exactly the same problem after upgraded python to 3.9 on windows .在 windows 上将 python 升级到 3.9 后,我遇到了完全相同的问题。 After struggling for hours, I managed to solve it by manually copying some dlls from ***/mingw/bin/ where mingw32-g++ is found to where my ***.pyd is located.经过几个小时的努力,我设法通过从 ***/mingw/bin/ 中手动复制一些 dll 来解决它,其中 mingw32-g++ 位于我的 ***.pyd 所在的位置。 I'm sure that ***/mingw/bin/ has been appended to %PATH%, but don't know why python3.9 couldn't find it.我确定 ***/mingw/bin/ 已附加到 %PATH%,但不知道为什么 python3.9 找不到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入 pyrfc 时 DLL 加载失败:找不到指定的模块 - DLL load failed while importing pyrfc: The specified module could not be found DLL加载失败:导入scipy时找不到指定的模块 - DLL load failed: The specified module could not be found while importing scipy 导入 Cartopy 时 ImportError: DLL load failed while importing trace: The specified module could not be found - When importing Cartopy ImportError: DLL load failed while importing trace: The specified module could not be found 导入 Dash 时出错:“ImportError: DLL load failed while importing _brotli: The specified module could not be found.” - Error when importing Dash: "ImportError: DLL load failed while importing _brotli: The specified module could not be found." ImportError: DLL 在导入 cv2 时加载失败:在 pycharm 中找不到指定的模块,python 3.9 - ImportError: DLL load failed while importing cv2: The specified module could not be found in pycharm with python 3.9 Windows7 64-bit, Pyside6, PyQt6, ImportError: DLL load failed while importing QtCore: The specified procedure could not be found - Windows7 64-bit, Pyside6, PyQt6, ImportError: DLL load failed while importing QtCore: The specified procedure could not be found python Windows“ ImportError:DLL加载失败:找不到指定的模块。”加载编译的扩展名时 - python Windows “ImportError: DLL load failed: The specified module could not be found.” When loading compiled extension cx_Freeze ImportError: DLL load failed while importing _ctypes: 找不到指定的模块 - cx_Freeze ImportError: DLL load failed failed while importing _ctypes: The specified module could not be found 加载 psycopg2 模块时出错:导入 _psycopg 时 DLL 加载失败:找不到指定的模块 - Error loading psycopg2 module: DLL load failed while importing _psycopg: The specified module could not be found 从 Python 外部运行 Python 脚本错误:导入 etree 时 DLL 加载失败:找不到指定的模块 - Running Python Script from outside of Python Error: DLL load failed while importing etree: The specified module could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM