问题描述
所以我有一些Python的C扩展我已经previously专为在32位的Python在Win7运行使用。我现在已经但是切换到64位Python和我有问题构建C扩展使用MinGW-W64。
So I have a few Python C extensions I have previously built for and used in 32 bit Python running in Win7. I have now however switched to 64 bit Python, and I am having issues building the C extension with MinGW-w64.
我所做的更改的distutils按这个帖子,但我得到一些奇怪的错误提示什么是错的:
I made the changes to distutils as per this post, but I am getting some weird errors suggesting something is wrong:
$ python setup.py build
running build
running build_ext
building 'MyLib' extension
c:\MinGW64\bin\x86_64-w64-mingw32-gcc.exe -mdll -O -Wall -Ic:\Python27\lib\site-packages\numpy\core\include -Ic:\Python27\include -Ic:\Python27\PC -c MyLib.c -o build\temp.win-amd64-2.7\Release\mylib.o
MyLib.c: In function 'initMyLib':
MyLib.c:631:5: warning: implicit declaration of function 'Py_InitModule4_64' [-Wimplicit-function-declaration]
writing build\temp.win-amd64-2.7\Release\MyLib.def
c:\MinGW64\bin\x86_64-w64-mingw32-gcc.exe -shared -s build\temp.win-amd64-2.7\Release\mylib.o build\temp.win-amd64-2.7\Release\MyLib.def -Lc:\Python27\libs -Lc:\Python27\PCbuild\amd64 -lpython27 -o build\lib.win-amd64-2.7\MyLib.pyd
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x13d): undefined reference to `__imp_PyExc_ValueError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1275): undefined reference to `__imp_PyExc_ValueError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1eef): undefined reference to `__imp_PyExc_ImportError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1f38): undefined reference to `__imp_PyExc_AttributeError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1f4d): undefined reference to `__imp_PyCObject_Type'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1f61): undefined reference to `__imp_PyExc_RuntimeError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1fc7): undefined reference to `__imp_PyExc_RuntimeError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x1ffe): undefined reference to `__imp_PyExc_RuntimeError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x2042): undefined reference to `__imp_PyExc_RuntimeError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x206c): undefined reference to `__imp_PyExc_RuntimeError'
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x208a): more undefined references to `__imp_PyExc_RuntimeError' follow
build\temp.win-amd64-2.7\Release\mylib.o:MyLib.c:(.text+0x20a7): undefined reference to `__imp_PyExc_ImportError'
collect2.exe: error: ld returned 1 exit status
error: command 'x86_64-w64-mingw32-gcc' failed with exit status 1
我用Google搜索周围相当多的查找信息,但它不容易找到一个明确的答案。可能有人阐明这光?我应该做哪些进一步的修改,能够成功地打造C扩展为64位的Python在Win7?
I have googled around quite a bit to find information, but it's not easy to find a definite answer. Could someone shed some light on this? What further changes should I do to be able to successfully build C extensions for 64 bit Python in Win7?
编辑:
在下面我设法产生cgohlke的评论一些有用的指针 libpython27.a
。下面就这个帖子(第二到最后一个)的意见后,但我仍然有一个在 __ imp_Py_InitModule4_64
错误。经过一番严肃的谷歌福,我设法在这篇文章跳闸告诉我重命名 Py_InitModule4
行 Py_InitModule4_64
。之后,一切都顺顺当当。
After some helpful pointers in cgohlke's comments below I managed to generate libpython27.a
. However after following the advice on this post (2nd to last) I still had a the __imp_Py_InitModule4_64
error. After some serious Google-fu I managed to trip over this post telling me to rename the Py_InitModule4
line to Py_InitModule4_64
. After that everything worked swimmingly.
推荐答案
这为我工作与Python 3.3:
This worked for me with Python 3.3 :
-
创建DLL静态的Python的lib
create static python lib from dll
蟒蛇DLL通常是C:/在Windows / System32下;在MSYS壳:
python dll is usually in C:/Windows/System32; in msys shell:
gendef.exe python33.dll
dlltool.exe --dllname python33.dll --def python33.def --output-lib libpython33.a
mv libpython33.a C:/Python33/libs
使用痛饮生成包装
use swig to generate wrappers
例如,痛饮-c ++ -python myExtension.i
包装必须符合MS_WIN64编译,或者当您导入类在Python你的电脑会崩溃
wrapper MUST be compiled with MS_WIN64, or your computer will crash when you import the class in Python
g++ -c myExtension.cpp -I/other/includes
g++ -DMS_WIN64 -c myExtension_wrap.cxx -IC:/Python33/include
共享库
shared library
g++ -shared -o _myExtension.pyd myExtension.o myExtension_wrap.o -lPython33 -lOtherSharedLibs -LC:/Python33/libs -LC:/path/to/other/shared/libs
确认所有的共享库(GDAL,OtherSharedLibs)是在PATH(Windows不使用LD_LIBRARY_PATH或PYTHONPATH)
make sure all shared libs (gdal, OtherSharedLibs) are in your PATH(windows does not use LD_LIBRARY_PATH or PYTHONPATH)
在Python,只是:进口myExtension
in Python, just: import myExtension
瞧!
这篇关于如何建立我的C扩展使用MinGW-W64在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!