所以我有一些Python C扩展,我以前已经为Win7中运行的32位Python使用。然而,我现在已经转向使用64位Python,而我在用MinGW-w64构建C扩展时遇到了问题。如何在Python中使用MinGW-w64构建我的C扩展?
我所做的更改的distutils按照this post,但我得到一些奇怪的错误提示什么是错的:
$ 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搜索周围相当多的查找信息,但它不容易找到一个确定的答案。有人可以对此有所了解吗?我应该做些什么才能在Win7中成功构建64位Python的C扩展?
编辑:
在cgohlke的评论一些有用的指针下面我设法生成libpython27.a
后。但是,在遵循this post(倒数第二)的建议后,我仍然有__imp_Py_InitModule4_64
错误。在一些严重的Google-fu之后,我设法跳过this post告诉我将Py_InitModule4
行重命名为Py_InitModule4_64
。之后,一切都顺利进行。
您需要使用'gendef.exe python27.dll'和'dlltool.exe --dllname python27.dll --def python27.def --output-lib libpython27.a'创建'libpython27.a'并放置它在'C:\ Python27 \ libs' – cgohlke
我看到一些提到gendef的帖子。但找到关于如何生成libpython27.a文件的信息并不容易。你会碰巧知道吗? – c00kiemonster
呵呵,gendef和dlltool是两个不同的命令。会尝试。谢谢。 – c00kiemonster