GCC11版基本完成了C++20特性的实现,本文讲解如何在Ubuntu18.04下构建支持C++20的g++编译程序。

 

编译GCC11

#GCC11下载镜像地址

(推荐)https://mirrors.ustc.edu.cn/gnu/gcc/     清华大学GNU镜像

http://www.gnu.org/prep/ftp.html    GNU镜像列表

 

#下载

#下载地址
#gcc 11.1
路径1(推荐):https://mirrors.ustc.edu.cn/gnu/gcc/gcc-11.1.0/gcc-11.1.0.tar.xz
路径2:https://ftp.gnu.org/gnu/gcc/gcc-11.1.0/gcc-11.1.0.tar.gz
解压后,尝试生成Makefile 

提示需要安装:GMP、MPFR、MPC依赖库,那就先安装这几个依赖库

 

#工具安装

下列某些工具如果已经安装在系统中则忽略

apt install vim
apt install wget
apt install gcc
apt install gcc-c++
#基础编译软件 
apt install m4
apt install texinfo
#编译gcc时候用到的
apt install zip

 

#依赖库 

#automake 1.15
#gmp-4.3.2
#mpfr-3.1.4
#mpc-1.0.3
 
### 下载地址
## automake-1.15
(Ubuntu 18.04自带,无需下载安装)
路径1(推荐):$ wget https://mirrors.ustc.edu.cn/gnu/automake/automake-1.5.tar.gz
路径2:$ wget ftp://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz
 
## gmp
路径1(推荐):$ wget https://mirrors.ustc.edu.cn/gnu/gmp/gmp-4.3.2.tar.bz2
路径2:$ wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2
 
## mpfr
路径1(推荐):$ wget https://mirrors.ustc.edu.cn/gnu/mpfr/mpfr-3.1.4.tar.bz2
路径2:$ wget https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
 
## mpc-1.0.3
路径1(推荐):$ wget https://mirrors.ustc.edu.cn/gnu/mpc/mpc-1.0.3.tar.gz
路径2:$ wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
 

安装GCC11构建依赖库

## 挨个解压gmp、mpfr、mpc包
#  tar -xjf gmp-4.3.2.tar.bz2
#  tar -xzvf mpc-1.0.3.tar.gz
 
 
## install automake
(Ubuntu 18.04自带,无需构建安装,跳过此步)
$ cd automake-1.15
$ ./configure --prefix=/usr/local/automake-1.15
$ make -j4
$ sudo make install 
#rpm -e --nodeps automake
#如果有卸载老版本,没有删除/usr/bin/automake
$ rm -rf automake
$ ln -s /usr/local/automake-1.15/bin/automake /usr/bin/automake
#edit export path
$ echo export PATH=$PATH:/usr/local/automake-1.15/bin >> /etc/profile
 
 
## install gmp
$ cd ../gmp-4.3.2
$ ./configure  --prefix=/usr/local/gmp-4.3.2/
$ make -j4
$ sudo make install
 
## install mpfr
$ cd  ../mpfr-3.1.4
$ ./configure --prefix=/usr/local/mpfr-3.1.4/ --with-gmp=/usr/local/gmp-4.3.2/
$ make -j4
$ sudo make install 
 
 
## install mpc
$ cd ../mpc-1.0.3
$ ./configure --prefix=/usr/local/mpc-1.0.3 --with-gmp=/usr/local/gmp-4.3.2/ --with-mpfr=/usr/local/mpfr-3.1.4/
$ make -j4
$ sudo make install
 
 
## gmp、mpfr、mpc库路径加入系统库变量中
$ echo export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc-1.0.3/lib:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-3.1.4/lib >> ~/.bashrc
$ source ~/.bashrc
 
或者
 
(如果没有写入权限,使用sudo vim /etc/profile直接编辑)
$ echo export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc-1.0.3/lib:/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-3.1.4/lib >> /etc/profile
$ source /etc/profile

 

GCC11构建

$ ll
-rw-rw-r--  1 duo duo 131174790 6月  19 11:29 gcc-11.1.0.tar.xz
 
# 解压
$ tar -xzvf gcc-11.1.0.tar.xz
 
# 进入
$ cd gcc-11.1.0
 
# 生成Makefile
$ ./configure --prefix=/usr/local/gcc-11.1.0/ --enable-threads=posix --disable-checking --enable--long-long --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-4.3.2/ --with-mpfr=/usr/local/mpfr-3.1.4/ --with-mpc=/usr/local/mpc-1.0.3/
 
# 构建
$ make -j4
(如果提示找不到"mpfr.so.4"等mpfr、mpc、gmp动态链接库文件,是因为“#依赖库”步骤中的这三个库文件的路径没有添加到“~/.bashrc”或者“/etc/profile”文件中)
大概需要1.5个小时 (Intel i5 2核4线程)

 

GCC11安装

$ sudo make install


安装到了"/usr/local/gcc-11.1.0"下,而且要保证gmp、mpc、mpfr这三个软件库不能删除!

# 创建软连接,方便使用gcc11.1,g++11.1命令

## add to /usr/bin
 
$ ln -s /usr/local/gcc-11.1.0/bin/gcc /usr/bin/gcc11.1
$ ln -s /usr/local/gcc-11.1.0/bin/g++ /usr/bin/g++11.1
 
## 键入gcc11 --version 查看版本
$ gcc11.1 -v
$ g++11.1 -v
 

测试g++11.1

modules功能参看我的博文: 【C++20】module特性测试

 

参考:

https://blog.csdn.net/whq12789/article/details/106381601   GCC10.1 构建方法

https://zhuanlan.zhihu.com/p/350136757 测试modules代码

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐