# Ubuntu下安装GCC

为了支持新的c++标准,需要安装新的GCC,安装步骤如下.

# 查看官方安装指导

GCC版本查看,截止20240324gcc最新的版本为13.2,支持到了c++23标准。

版本发布信息可以参考:GCC Releases (opens new window)查看。

安装指导参考的网页为:【GCC Installation Instructions (opens new window)

  • 检查安装依赖,
  • 下载源码,可使用git命令克隆
git clone git://gcc.gnu.org/git/gcc.git
# 查看所有分支
git branch -a
# 查看所有标签
git tag -l

有时候,国内访问gcc放置代码的git仓库速度会很慢,这个时候可以从开源中国的码云平台上下载,

https://gitee.com/mirrors/gcc.git

这个是国内镜像,可以加速下载速度。

  • 下载源码后,切入源码目录,执行如下命令下载依赖
./contrib/download_prerequisites
  • 配置,可在源码仓库下新建build目录,然后切换到build目录下执行如下命令进行配置。
../configure --enable-languages=c,c++ --prefix=/usr/local/gcc-13.2.0 --host=x86_64-pc-linux-gnu --prefix=/home/xx/data/sw/gcc13 --disable-multilib

更多的配置参数可以参考页面https://gcc.gnu.org/install/configure.html (opens new window)

  • 安装
make -j4
sudo make install
  • 验证安装
/usr/local/gcc-13.2.0/bin/gcc --version
  • 设置环境变量
export PATH=$PATH:/usr/local/gcc-13.2.0/bin
  • 卸载
sudo rm -rf /usr/local/gcc-13.2.0

# 错误

# 缺少gmp

xx@xx-rob:~/data/code/gcc$ ./configure --enable-languages=c,c++ --prefix=/usr/local/gcc-13.2.

checking for the correct version of gmp.h... no
configure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations.  Source code for these libraries can be found at
their respective hosting sites as well as at

解决方法,先执行命令:

xx@xx-rob:~/data/code/gcc$ ./contrib/download_prerequisites

2024-03-21 22:31:18 URL:http://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2 [2493916/2493916] -> "gmp-6.2.1.tar.bz2" [1]
2024-03-21 22:33:48 URL:http://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.1.0.tar.bz2 [1747243/1747243] -> "mpfr-4.1.0.tar.bz2" [1]

2024-03-21 22:34:29 URL:http://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.2.1.tar.gz [838731/838731] -> "mpc-1.2.1.tar.gz" [1]
2024-03-21 22:35:49 URL:http://gcc.gnu.org/pub/gcc/infrastructure/isl-0.24.tar.bz2 [2261594/2261594] -> "isl-0.24.tar.bz2" [1]
gmp-6.2.1.tar.bz2: OK
mpfr-4.1.0.tar.bz2: OK
mpc-1.2.1.tar.gz: OK
isl-0.24.tar.bz2: OK
All prerequisites downloaded successfully.

# 缺少32位开发库libc

/usr/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status
configure: error: I suspect your system does not have 32-bit development libraries (libc and headers). If you have them, rerun configure with --enable-multilib. If you do not have them, and want to build a 64-bit-only compiler, rerun configure with --disable-multilib.

解决办法:

  • 安装32libc库文件
  • ./configure时使用参数--disable-multilib禁用编译32位平台上可以使用的库

# g++: error: gengtype-lex.c: No such file or directory

编译过程中报错:

> and I found the Makefile in my objdir directory. I try `make -j4` and found
> g++: error: gengtype-lex.c: No such file or directory
> g++: fatal error: no input files
> Could you help me with this?

解决办法:

缺少flex库文件,手动安装:

sudo apt-get install flex

# 编译报错:libstdc++.so.6: version `GLIBCXX_3.4.20' not found


# lib/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /usr/lib/x86_64-linux-gnu/libproxy.so.1)
# Failed to load module: /usr/lib/x86_64-linux-gnu/gio/modules/libgiolibproxy.so

运行程序时报以上错误,这说明GCC使用的依赖库太老了,可以使用以下命令查看c++库支持的GLIBCXX版本,

strings /usr/lib/libstdc++.so.6 | grep GLIBCXX

解决办法,安装新版本的gcc,并在新版本的lib64文件夹下找到libstdc++库文件,将该路径添加到链接库路径:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/cur/xx

# reference

1.https://www.spinics.net/lists/gcchelp/msg50998.html (opens new window)
2.https://stackoverflow.com/questions/44773296/libstdc-so-6-version-glibcxx-3-4-20-not-found (opens new window)