【发布时间】:2012-06-25 02:12:27
【问题描述】:
所以我有一些 Python C 扩展,我之前构建并用于在 Win7 中运行的 32 位 Python。但是,我现在已经切换到 64 位 Python,并且在使用 MinGW-w64 构建 C 扩展时遇到了问题。
我根据this post 对 distutils 进行了更改,但我收到了一些奇怪的错误提示:
$ 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
我已经搜索了很多信息以查找信息,但要找到明确的答案并不容易。有人可以对此有所了解吗?为了能够在 Win7 中成功构建 64 位 Python 的 C 扩展,我应该做哪些进一步的更改?
编辑:
在下面的 cgohlke 的 cmets 中提供了一些有用的指针后,我设法生成了 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中 -
我看到一些帖子提到了gendef。但是要找到有关如何生成 libpython27.a 文件的信息并不容易。你会碰巧知道吗?
-
哦,好的,gendef 和 dlltool 是两个不同的命令。会尝试。谢谢。
-
好的,它编译得很好,但是当我尝试导入模块时,我得到
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: DLL load failed: The specified procedure could not be found.我想我一定是在某个地方丢失了一个文件。我对 gendef 所做的唯一事情就是将libpython27.a放入C:\Python27\libs。我错过了什么吗? -
所以我按照this 帖子(倒数第二个)完成了所有步骤。但现在它不会编译,我得到这个
build\temp.win-amd64-2.7\Release\cquant.o:cQuant.c:(.text+0x1f09): undefined reference to__imp_Py_InitModule4_64' collect2.exe: error: ld returned 1 exit status`
标签: python c compilation 64-bit