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

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

我想使用 Swig 将我的 C/C++ "model-controler" 应用程序与 Python HMI 结合使用。

我首先在我的 ubuntu PC 上测试了 python swig 简单示例,它非常有效。

然后我尝试在带有 Mingw-64 的 Win7-64 位系统上做同样的事情:
"Wrap" 通过使用

swig.exe -python path_to_swig_examples\python\simple\example

在cmd window中生成example_wrap.c... 好的

然后在mingw终端下编译(我个人是在资源管理器中右击启动然后GitBash这里

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

生成我的对象…好的
和 link

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'

我将link添加到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未定义的消息消失了……但我还有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

终于,它成功了......在花了很多时间之后!

版本:
Windows 7 sp1
痛饮 3.0.12
明W-64
Anaconda3 4.4.0
gcc 6.2.0 / ld 2.25