错误信息
g++: error: unrecognized command line option ‘-std=gnu++14’
-std=c++14需要g++5.2以上,而centos默认的g++只有4.8.5,所以,要做的事情,是升级g++
下载gcc
- 官网下载:
https://ftp.gnu.org/gnu/gcc/
wget https://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz
- 解压:
tar zxvf gcc-13.2.0.tar.gz
- 直接编译会报错
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 https://gcc.gnu.org/pub/gcc/infrastructure/. See also http://gcc.gnu.org/install/prerequisites.html for additional info. If you obtained GMP, MPFR and/or MPC from a vendor distribution package, make sure that you have installed both the libraries and the header files. They may be located in separate packages.
- 查看gcc的依赖:
cat ./contrib/download_prerequisites
,最上面的位置
gmp='gmp-6.2.1.tar.bz2'
mpfr='mpfr-4.1.0.tar.bz2'
mpc='mpc-1.2.1.tar.gz'
isl='isl-0.24.tar.bz2'
下载gcc依赖
- 官网地址:
https://gcc.gnu.org/pub/gcc/infrastructure/
- GMT:
wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
- MPFR:
wget https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.1.0.tar.bz2
- MPC:
wget https://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.2.1.tar.gz
- ISL:
wget https://gcc.gnu.org/pub/gcc/infrastructure/isl-0.24.tar.bz2
解压依赖
tar jxvf gmp-6.2.1.tar.bz2
tar jxvf mpfr-4.1.0.tar.bz2
tar zxvf mpc-1.2.1.tar.gz
tar jxvf isl-0.24.tar.bz2
编译依赖
cd gmp-6.2.1/
./configure --prefix=/usr/local/gmp-6.2.1
make && sudo make install
cd ../mpfr-4.1.0/
./configure --prefix=/usr/local/mpfr-4.1.0 --with-gmp=/usr/local/gmp-6.2.1
make && sudo make install
cd ../mpc-1.2.1/
./configure --prefix=/usr/local/mpc-1.2.1 --with-gmp=/usr/local/gmp-6.2.1 --with-mpfr=/usr/local/mpfr-4.1.0
make && sudo make install
升级步骤
# 进入gcc目录安装
cd gcc-13.2.0
# 下载某些依赖包,如果内网服务器不能上外网,就根据上面的步骤手动下载
./contrib/download_prerequisites
# 创建bulid文件夹
mkdir build
cd build
../configure --prefix=/usr/local/gcc --enable-languages=c,c++ --disable-multilib --with-gmp=/usr/local/gmp-6.2.1 --with-mpfr=/usr/local/mpfr-4.1.0 --with-mpc=/usr/local/mpc-1.2.1
# 编译gcc过程如果报找不到libmpfr.so.6,则执行以下2条命令
ln -s /usr/local/mpfr-4.1.0/lib/libmpfr.so.6.1.0 /usr/lib/libmpfr.so.6
ldconfig
# 编译安装,此过程耗时较长,建议使用后台运行
nohup make && make install &
# 修改软连接
mv /usr/bin/gcc /usr/bin/gcc_bak
ln -s /usr/local/gcc/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++_bak
ln -s /usr/local/gcc/bin/g++ /usr/bin/g++
# 查看升级后版本
gcc --version
g++ --version
安装成功截图
问题处理
error while loading shared libraries: libmpfr.so.6: cannot open shared object file: No such file or directory
ln -s /usr/local/mpfr-4.1.0/lib/libmpfr.so.6.1.0 /usr/lib/libmpfr.so.6.1.0
ldconfig
# 然后重新安装gcc