SWIG和Mingw-64与Python一起使用...不是有效的 Win32 应用程序

SWIG and Mingw-64 to use with Python … not a valid Win32 application

本文关键字:有效 应用程序 Win32 Mingw-64 Python 一起 SWIG      更新时间:2023-10-16

我想使用Swig将我的C/C++"模型控制器"应用程序与Python HMI一起使用。

我首先在我的 ubuntu PC 上测试 python swig 简单示例,它就像一个魅力。

然后我尝试使用 Mingw-64 在 Win7-64 位上做同样的事情:
使用

swig.exe -python path_to_swig_examplespythonsimpleexample

在cmd窗口中,它会生成example_wrap.c...还行

然后在 mingw 终端下编译(我个人通过在资源管理器中右键单击然后在这里使用 Git Bash启动(

gcc -c -fPIC -DWIN32 example.c example_wrap.c -I/C/Program Files (x86)/Python36-32/include

生成我的对象...好的
和链接

ld -shared example.o example_wrap.o -o _example.dll

我有很多undefined reference

example_wrap.o:example_wrap.c:(.text+0xe3): undefined reference to `strlen'
example_wrap.o:example_wrap.c:(.text+0x1de): undefined reference to `strcmp'
example_wrap.o:example_wrap.c:(.text+0x5ff): undefined reference to `strcmp'
example_wrap.o:example_wrap.c:(.text+0x957): undefined reference to `strlen'
example_wrap.o:example_wrap.c:(.text+0x98f): undefined reference to `strcpy'
example_wrap.o:example_wrap.c:(.text+0x9c8): undefined reference to `strcmp'
example_wrap.o:example_wrap.c:(.text+0xa38): undefined reference to `strlen'
example_wrap.o:example_wrap.c:(.text+0xaaf): undefined reference to `strncpy'
example_wrap.o:example_wrap.c:(.text+0xaf5): undefined reference to `strcmp'
example_wrap.o:example_wrap.c:(.text+0xb11): undefined reference to `memset'
example_wrap.o:example_wrap.c:(.text+0xb57): undefined reference to `__imp_PyUnicode_AsUTF8String'
example_wrap.o:example_wrap.c:(.text+0xb76): undefined reference to `__imp_PyBytes_AsStringAndSize'
example_wrap.o:example_wrap.c:(.text+0xb88): undefined reference to `malloc'
example_wrap.o:example_wrap.c:(.text+0xbaa): undefined reference to `memcpy'
example_wrap.o:example_wrap.c:(.text+0xc15): undefined reference to `__imp_PyUnicode_FromString'
example_wrap.o:example_wrap.c:(.text+0xc69): undefined reference to `__imp_PyExc_MemoryError'
example_wrap.o:example_wrap.c:(.text+0xc7c): undefined reference to `__imp_PyExc_IOError'
example_wrap.o:example_wrap.c:(.text+0xc8f): undefined reference to `__imp_PyExc_RuntimeError'
example_wrap.o:example_wrap.c:(.text+0xca2): undefined reference to `__imp_PyExc_IndexError'

我添加了指向python36的链接.dll(基于Anaconda3:https://www.continuum.io/downloads(/c/ProgramData/Anaconda3/python36.dll

ld -shared example.o example_wrap.o/c/ProgramData/Anaconda3/python36.dll -o _example.dll

和 python undefined msg 消失了...但我仍然有 C/C++ 函数undefined reference... 。

ld -shared example.o example_wrap.o/c/ProgramData/Anaconda3/python36.dll/c/Windows/System32/msvcr120.dll -o _example.dll

和dll被创建!

感谢您所有可用的 swig+python+mingw 编译信息都过时了吗?

。DLL 已创建,但不起作用:
"ModuleNotFoundError: No module named '_example' "

没有名为_example的模块,
然后将 DLL 重命名为 PYD...
ImportError: DLL load failed: %1 is not a valid Win32 application
......是因为它是我运行的 32 位的 python。

启动 anaconda python:

C:Program DataAnaconda3python.exe
Python 3.5.1 |Anaconda 4.4.0 (64-bit)…
>>> import example
>>> example.gcd(42,36)
6
>>> example.cvar.Foo
3.0

最后,它有效...经过很多时间!

版本:
视窗 7 sp1
斯威格 3.0.12
明W-64
蟒蛇3 4.4.0 gcc 6.2.0
/ld 2.25

相关文章: