编译GLIBC源码遇__builtin_roundeven隐式函数声明错误求助
问题详情
编译GLIBC时触发如下编译错误:
../sysdeps/ieee754/flt-32/e_gammaf_r.c: In function ‘__ieee754_gammaf_r’:../sysdeps/ieee754/flt-32/e_gammaf_r.c:127:32: error: implicit declaration of function ‘__builtin_roundeven’; did you mean ‘__builtin_round’? [-Werror=implicit-function-declaration]
double m = z - 0x1.7p+1, i = __builtin_roundeven(m), step = __builtin_copysign(1.0,i);
^~~~~~~~~~~~~~~~~~~
__builtin_round
cc1: all warnings being treated as errors
使用的编译流程命令:
git clone https://sourceware.org/git/glibc.git glibc-devel cd glibc-devel mkdir build cd build ../configure --prefix=/home/a0504063/bin/python/glibc --host=x86_64-linux-gnu --build=x86_64-linux-gnu CC="gcc -m64" CXX="g++ -m64" CFLAGS="-O2 -Wno-error" CXXFLAGS="-O2 -Wno-error" make
尝试通过-Wno-error关闭“警告转错误”规则,但该参数被GLIBC默认编译选项覆盖,未生效。
环境信息:
- GLIBC提交哈希:18596c5415
- GCC/G++版本:8.5.0
- 操作系统:RedHat 8(无法更换)
解决方法
方法一:正确传递编译参数
GLIBC的configure脚本会将用户指定的CFLAGS前置,而默认选项中包含-Werror,导致-Wno-error被覆盖。需要用追加的方式传递参数:
../configure --prefix=/home/a0504063/bin/python/glibc --host=x86_64-linux-gnu --build=x86_64-linux-gnu CC="gcc -m64" CXX="g++ -m64" CFLAGS="$CFLAGS -O2 -Wno-error" CXXFLAGS="$CXXFLAGS -O2 -Wno-error"
执行完configure后再重新运行make。
方法二:修改源码适配旧版GCC
GCC 8.x不支持__builtin_roundeven内置函数(该函数在GCC 9及以上版本引入),直接修改对应源码:
- 打开文件
../sysdeps/ieee754/flt-32/e_gammaf_r.c - 找到第127行,将
__builtin_roundeven(m)替换为__builtin_round(m) - 保存文件后重新执行
make
方法三:切换到兼容GCC 8的GLIBC版本
当前使用的GLIBC提交已不再兼容GCC 8,可切换到稳定兼容分支(如glibc-2.35):
cd ../glibc-devel git checkout glibc-2.35 cd build rm -rf ./* ../configure --prefix=/home/a0504063/bin/python/glibc --host=x86_64-linux-gnu --build=x86_64-linux-gnu CC="gcc -m64" CXX="g++ -m64" CFLAGS="-O2" make
内容的提问来源于stack exchange,提问作者Benjamin Davis

