You need to enable JavaScript to run this app. Cython生成C代码在MinGW-w64下编译遇未定义引用与重定位错误
优惠活动
大模型
产品
解决方案
定价
更多

Cython生成C代码在MinGW-w64下编译遇未定义引用与重定位错误

Windows环境下Cython生成C代码的mingw编译链接报错

基础环境

  • 操作系统:Windows 10
  • Python版本:3.8.4 x86_64-w64-mingw32
  • 编译工具链:mingw32-w64(当前gcc版本4.8.3)
  • 编译对象:Cython将Python Hello World代码转换生成的C源文件

已执行的编译命令

gcc -DMS_WIN64 -I Python38\include -o test.exe test.c -lpython38 -lm -L Python38\libs -shared -Wl,--enable-auto-image-base -Wl,--enable-auto-import

链接阶段完整报错

C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x28c): undefined reference to `__imp_PyExc_ImportError'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x307): undefined reference to `__imp__Py_NoneStruct'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x34c): undefined reference to `__imp_PyExc_AttributeError'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x59c): undefined reference to `__imp_PyExc_RuntimeError'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xb51): undefined reference to `__imp_PyExc_ImportError'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xc23): undefined reference to `__imp_PyExc_NameError'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xcb9): undefined reference to `__imp__Py_CheckRecursionLimit'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xd22): undefined reference to `__imp__Py_CheckRecursionLimit'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xd32): undefined reference to `__imp__Py_CheckRecursionLimit'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xd40): undefined reference to `__imp__Py_CheckRecursionLimit'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xd8a): undefined reference to `__imp_PyExc_SystemError'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xfc4): undefined reference to `__imp__Py_FalseStruct'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0xfcd): undefined reference to `__imp__Py_TrueStruct'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x101f): undefined reference to `__imp__Py_FalseStruct'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x1037): undefined reference to `__imp__Py_FalseStruct'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x1044): undefined reference to `__imp__Py_TrueStruct'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x17c0): undefined reference to `__imp_PyModule_Type'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x17d8): undefined reference to `__imp_PyModule_Type'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x18d4): undefined reference to `__imp_PyBaseObject_Type'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x1e92): undefined reference to `__imp_PyExc_DeprecationWarning'
C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o:test.c:(.text+0x1ee9): undefined reference to `__imp_PyExc_TypeError'
c:/users/ja/documents/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Ja\AppData\Local\Temp\ccqKHihT.o: bad reloc address 0x0 in section `.data'
collect2.exe: error: ld returned 1 exit status

报错核心原因

  • 参数逻辑冲突:将输出目标设置为可执行文件test.exe的同时,加入了-shared(生成动态链接库)参数,直接导致目标文件的重定位段格式不符合可执行文件规范,触发.data段的重定位地址错误。
  • 导入库格式不兼容:Python官方安装包附带的python38.lib是为MSVC编译器生成的导入库,mingw-w64链接器无法解析其符号导出表,因此会报出大量带__imp_前缀的Python API符号未定义的链接错误。
  • 工具链版本滞后:当前使用的gcc 4.8.3版本发布时间较早,对64位PE格式的Python 3.8适配性不足,会进一步放大重定位解析的兼容问题。

修正方案

  • 生成mingw专属的Python导入库:使用mingw自带的gendef工具读取Python安装目录下的python38.dll,再通过dlltool工具生成符合mingw链接规则的libpython38.a文件,替换原有MSVC格式的导入库。
  • 调整编译参数:
    • 若编译目标是Python可加载的扩展模块,将输出名从test.exe修改为test.pyd,保留-shared相关参数,同时调整参数顺序,把源文件、库路径参数放在链接命令的前半段,依赖库参数放在后半段,避免链接器符号遍历顺序错误。
    • 若编译目标是独立可执行文件,移除-shared及对应的--enable-auto-*链接参数,配置正确的程序入口参数。
  • 升级mingw-w64工具链:替换当前的4.8.3老旧版本为10.0及以上的稳定版本,消除PE文件格式解析的兼容问题。

内容的提问来源于stack exchange,提问作者kacpo1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.05 16:57:03