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

Ubuntu 22.04源码编译安装g++-4.8.5报错Error 2如何解决

Ubuntu 22.04 编译安装g++ 4.8.5失败问题

问题描述

在Ubuntu 22.04系统上安装4.8.5版本g++编译器时,参考了一篇Ubuntu 20.04下同版本编译器的安装教程,按照教程给出的所有修改步骤操作后,依然无法完成gcc 4.8.5编译。
执行的操作步骤如下:

sudo dpkg --add-architecture i386
sudo apt update
sudo apt upgrade
sudo apt-get install gcc-multilib libstdc++6:i386
wget https://ftp.gnu.org/gnu/gcc/gcc-4.8.5/gcc-4.8.5.tar.bz2 --no-check-certificate
tar xf gcc-4.8.5.tar.bz2
# cd gcc-4.8.5
# ./contrib/download_prerequisites
# cd ..
sed -i -e 's/__attribute__/\/\/__attribute__/g' gcc-4.8.5/gcc/cp/cfns.h
sed -i 's/struct ucontext/ucontext_t/g' gcc-4.8.5/libgcc/config/i386/linux-unwind.h
sed -i '/#include <pthread.h>/a #include <signal.h>' path_to_gcc4.8.5src/libsanitizer/asan/asan_linux.cc
sed -i 's/__res_state \*statp = (__res_state\*)state\;/struct __res_state \*statp = (struct __res_state\*)state\;/g' path_to_gcc4.8.5src/libsanitizer/tsan/tsan_platform_linux.cc
mkdir xgcc-4.8.5
pushd xgcc-4.8.5
$PWD/../gcc-4.8.5/configure --enable-languages=c,c++ --prefix=/usr --enable-shared --enable-plugin --program-suffix=-4.8.5
make MAKEINFO="makeinfo --force" -j
sudo make install -j

编译过程触发报错,最终返回Error 2终止,报错信息如下:

/home/umtts/xgcc-4.8.5/../gcc-4.8.5/gcc/config/i386/i386.c: In function ‘tree_node* ix86_handle_abi_attribute(tree_node**, tree, tree, int, bool*)’:
/home/umtts/xgcc-4.8.5/../gcc-4.8.5/gcc/config/i386/i386.c:34858:18: warning: unquoted identifier or keyword ‘ms_abi’ in format [-Wformat-diag]
34858 |           error ("ms_abi and sysv_abi attributes are not compatible");
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/umtts/xgcc-4.8.5/../gcc-4.8.5/gcc/config/i386/i386.c:34858:18: warning: unquoted identifier or keyword ‘sysv_abi’ in format [-Wformat-diag]
/home/umtts/xgcc-4.8.5/../gcc-4.8.5/gcc/config/i386/i386.c:34867:18: warning: unquoted identifier or keyword ‘ms_abi’ in format [-Wformat-diag]
34867 |           error ("ms_abi and sysv_abi attributes are not compatible");
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/umtts/xgcc-4.8.5/../gcc-4.8.5/gcc/config/i386/i386.c:34867:18: warning: unquoted identifier or keyword ‘sysv_abi’ in format [-Wformat-diag]
/home/umtts/xgcc-4.8.5/../gcc-4.8.5/gcc/config/i386/i386.c: In function ‘long unsigned int ix86_memmodel_check(long unsigned int)’:
/home/umtts/xgcc-4.8.5/../gcc-4.8.5/gcc/config/i386/i386.c:42398:15: warning: unquoted identifier or keyword ‘HLE_ACQUIRE’ in format [-Wformat-diag]


/bin/bash /home/umtts/xgcc-4.8.5/../gcc-4.8.5/gcc/../move-if-change tmp-automata.c insn-automata.c
echo timestamp > s-automata
rm gcc.pod
make[3]: Leaving directory '/home/umtts/xgcc-4.8.5/gcc'
make[2]: *** [Makefile:4163: all-stage1-gcc] Error 2
make[2]: Leaving directory '/home/umtts/xgcc-4.8.5'
make[1]: *** [Makefile:21493: stage1-bubble] Error 2
make[1]: Leaving directory '/home/umtts/xgcc-4.8.5'
make: *** [Makefile:892: all] Error 2
umtts@wifi6:~/xgcc-4.8.5$ ls

解决方案

编译失败的核心原因有三个:一是原步骤中下载编译依赖的命令被注释,没有执行;二是Ubuntu 22.04默认自带的高版本gcc开启了-Wformat-diag等严格检查,老源码中的格式警告会被判定为问题终止编译;三是配置参数中开启了插件支持、安装路径设置为系统目录,和22.04系统环境不兼容。按以下步骤操作即可完成安装:

  • 清理旧的编译残留:直接删除之前解压的源码目录和创建的xgcc-4.8.5编译目录,重新解压gcc-4.8.5源码包,避免旧修改的干扰。
  • 执行依赖下载:进入重新解压的gcc-4.8.5源码目录,执行./contrib/download_prerequisites下载编译必需的依赖包,执行完后退回上层目录。
  • 打全兼容补丁:将以下命令中的GCC_SRC_PATH替换为你实际的gcc-4.8.5源码路径,依次执行:
GCC_SRC_PATH=./gcc-4.8.5
# 原有兼容补丁
sed -i -e 's/__attribute__/\/\/__attribute__/g' $GCC_SRC_PATH/gcc/cp/cfns.h
sed -i 's/struct ucontext/ucontext_t/g' $GCC_SRC_PATH/libgcc/config/i386/linux-unwind.h
sed -i '/#include <pthread.h>/a #include <signal.h>' $GCC_SRC_PATH/libsanitizer/asan/asan_linux.cc
sed -i 's/__res_state \*statp = (__res_state\*)state\;/struct __res_state \*statp = (struct __res_state\*)state\;/g' $GCC_SRC_PATH/libsanitizer/tsan/tsan_platform_linux.cc
# 新增补丁:关闭高版本gcc的格式检查报错,解决当前遇到的-Wformat-diag问题
sed -i 's/^WARN_CFLAGS = /WARN_CFLAGS = -Wno-format-diag -Wno-error /g' $GCC_SRC_PATH/gcc/Makefile.in
  • 重新配置编译参数:不要将安装路径设为/usr,会覆盖系统默认gcc导致异常,也不要开启--enable-plugin避免兼容问题,执行以下命令:
mkdir xgcc-4.8.5
pushd xgcc-4.8.5
$PWD/../gcc-4.8.5/configure --enable-languages=c,c++ --prefix=/usr/local/gcc-4.8.5 --enable-shared --disable-multilib --program-suffix=-4.8.5
# -j后面跟CPU核心数,避免并行编译顺序错误,$(nproc)会自动读取当前CPU核心数
make MAKEINFO="makeinfo --force" -j$(nproc)
sudo make install
  • 配置环境变量:执行以下命令将安装好的gcc 4.8.5加入系统路径:
echo 'export PATH=/usr/local/gcc-4.8.5/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/gcc-4.8.5/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

执行完后输入g++-4.8.5 -v,如果能正常输出版本信息就说明安装成功。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 06:09:29