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

Termux环境安装Python 3.11-dev出现getrandom编译报错如何解决

故障场景

在搭载Android 10 aarch64系统的安卓手机上使用Termux环境,通过pyenv安装Python 3.11-dev时编译失败,报错指向getrandom函数隐式声明不符合C99规范,完整编译输出如下:

pyenv install 3.11-dev
Cloning https://github.com/python/cpython...
Installing Python-3.11-dev...

BUILD FAILED (Linux 4.9.186-perf-g10af704 using python-build 20180424)

Inspect or clean up the working tree at /data/data/com.termux/files/usr/tmp/python-build.20211103205409.32581
Results logged to /data/data/com.termux/files/usr/tmp/python-build.20211103205409.32581.log

Last 10 log lines:
Python/bootstrap_hash.c:114:17: error: implicit declaration of function 'getrandom' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
            n = getrandom(dest, n, flags);
                ^
Python/bootstrap_hash.c:118:17: error: implicit declaration of function 'getrandom' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
            n = getrandom(dest, n, flags);
                ^
2 errors generated.
make: *** [Makefile:1960: Python/bootstrap_hash.o] Error 1
make: *** Waiting for unfinished jobs....
1 warning generated.
问题根因

Android平台使用的bionic libc对getrandom函数的声明规则与桌面端Linux的glibc不同,CPython 3.11开发版的源码没有适配该场景,未引入对应头文件就调用getrandom函数,触发了C99标准下的隐式声明编译错误。

解决方法
  • 方案1:添加编译参数后直接安装,执行如下命令即可:
    CFLAGS="-D_GNU_SOURCE -DHAVE_GETRANDOM=1" pyenv install 3.11-dev
    手动声明系统支持getrandom函数,跳过编译时的函数声明检测。
  • 方案2:若方案1无效,修改源码适配:
    1. 执行一次安装命令待报错后,保留生成的临时编译目录
    2. 进入日志中提示的源码目录,示例路径为/data/data/com.termux/files/usr/tmp/python-build.20211103205409.32581/Python-3.11-dev
    3. 编辑Python/bootstrap_hash.c文件,在头部头文件引入区添加一行#include <linux/random.h>
    4. 在源码目录下执行make && make install完成编译安装
  • 方案3:优先使用Termux官方适配的Python版本,执行pkg install python直接安装,不存在平台兼容问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 20:54:00