You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cython生成的C文件经GCC编译报错,寻求排查思路

问题:Cython生成的C文件在Cygwin GCC下编译失败

环境与操作步骤

  • 用Cython将Python代码转为C文件:
    cython --embed -o hello.c hello.py
    
  • 尝试编译生成的hello.c:
    gcc -Os -I <python_include_dir> -o hello hello.c -L <python_libs_dir> -lpython3 -lpthread -lm -lutil -ldl
    
  • 运行环境:Cygwin GCC 11.4.0、Python 3.11.2(Windows版本)

编译错误示例

In file included from C:\Users<user>\AppData\Local\Programs\Python\Python311\include/Python.h:38,
from hello.c:16:
C:\Users<user>\AppData\Local\Programs\Python\Python311\include/pyport.h:601:2: error: #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
601 | #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
| ^~~~~
error: expected expression before ‘int’
15508 | __PYX_VERIFY_RETURN_INT_EXC(int, PY_LONG_LONG, PyLong_AsLongLong(x))
error: expected ‘;’ before ‘zero’
15069 | func_type zero = 0;
note: in expansion of macro ‘__PYX_VERIFY_RETURN_INT_EXC’
15508 | __PYX_VERIFY_RETURN_INT_EXC(int, PY_LONG_LONG, PyLong_AsLongLong(x))

排查方向建议

  • 统一Python与编译环境的平台:当前使用的是Windows版Python,但编译工具是Cygwin GCC,两者ABI不兼容。建议通过Cygwin安装器安装python3、python3-devel和cython包,用Cygwin本地的Python和Cython重新生成C文件后再编译。
  • 修正编译参数中的库名:Cygwin下Python库的名称是具体版本号(比如python3.11),替换编译命令中的-lpython3为-lpython3.11,避免链接错误。
  • 使用正确的路径:如果改用Cygwin本地Python,python_include_dir应为Cygwin路径(如/usr/include/python3.11),python_libs_dir为/usr/lib,不要混用Windows系统路径。
  • 避免宏定义冲突:Windows Python的头文件依赖Windows平台宏定义,Cygwin环境下的宏(如LONG_BIT)与Windows不一致,导致头文件检查报错,这是核心矛盾,统一环境即可解决。
  • 不推荐的兼容尝试(仅作参考):若必须使用Windows Python,可尝试添加-D_WIN32或-U__CYGWIN__参数强制编译器使用Windows宏,但后续很大概率会出现链接或运行时问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 12:05:22