SWIG 和 Mingw-64 与 Python 一起使用……不是有效的 Win32 应用程序
Posted
技术标签:
【中文标题】SWIG 和 Mingw-64 与 Python 一起使用……不是有效的 Win32 应用程序【英文标题】:SWIG and Mingw-64 to use with Python … not a valid Win32 application 【发布时间】:2017-07-24 11:33:05 【问题描述】:我想使用 Swig 将我的 C/C++“模型控制器”应用程序与 Python HMI 一起使用。
我首先在我的 ubuntu PC 上测试 python swig 简单示例,它就像一个魅力。
然后我尝试在带有 Mingw-64 的 Win7-64 位上做同样的事情: 使用“包装”
swig.exe -python path_to_swig_examples\python\simple\example
在cmd窗口中,生成example_wrap.c…好的
然后在mingw终端下编译(我个人是在资源管理器中右键单击然后Git Bash here)
gcc -c -fPIC -DWIN32 example.c example_wrap.c -I/C/Program\ Files\ \(x86\)/Python36-32/include
生成我的 objs…好的 和链接
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'
【问题讨论】:
【参考方案1】:我添加了指向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 msgs 消失了……但我还有 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 !
感谢is all the available swig+python+mingw compile information outdated?
... dll 已创建,但不起作用:"ModuleNotFoundError: No module named '_example' "
swig no module named _example
然后将 dll 重命名为 pyd...ImportError: DLL load failed: %1 is not a valid Win32 application
…是因为它是我运行的 32 位 python。
启动 anaconda python:
C:\Program Data\Anaconda3\python.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 MingW-64 Anaconda3 4.4.0 gcc 6.2.0 / ld 2.25
【讨论】:
以上是关于SWIG 和 Mingw-64 与 Python 一起使用……不是有效的 Win32 应用程序的主要内容,如果未能解决你的问题,请参考以下文章
Windows MINGW64控制台上的Python3.6不运行解释器[重复]
Python for Windows 在调用 MinGW-w64 编译库的函数时挂起
Cpp调用Python3,使用matplotlib画(二维)图----1.配置(Clion,mingW64, python, matplotlib)