一、先卸载以前的旧版本
因为centOS默认自带4点几版本的gcc 、g++编译器,是比较老的版本的,所以先把老版本卸载掉。
1、 先获取root权限
2、 查看当前的安装版本号
rpm -q gcc
3、卸载掉旧版本
rpm -e [第二步查到的版本号]
4、第三步可能会遇到如下错误,卸载失败
error: Failed dependencies:
gcc = 4.4.7-23.el6 is needed by (installed) gcc-c++-4.4.7-23.el6.x86_64
gcc = 4.4.4 is needed by (installed) libtool-2.2.6-15.5.el6.x86_64
提示要卸载的版本有两个依赖,要先卸载掉这两个依赖(注意:下面要卸载的版本号要和上面提示的两个依赖的版本号一致)
rpm -e gcc-c++-4.4.7-23.el6.x86_64
rpm -e libtool-2.2.6-15.5.el6.x86_64
将这两个依赖卸载完成之后再次执行第3步就可卸载成功
5、验证卸载
[root@123 /]# gcc -v
bash: gcc: command not found
[root@123 /]# g++ -v
bash: g++: command not found
这样即为卸载成功
二、安装最新版本的gcc 、g++
在安装之前,可以先去查询一下目前gcc 、g++的版本以及区别,本文用了第11版
1、在系统中安装存储库
yum install centos-release-scl
2、安装gcc和g++包(注意版本号:11版)
yum install -y devtoolset-11-gcc devtoolset-11-gcc-c++
3、配置安装好的包(注意版本号:11版)
scl enable devtoolset-11 bash
4、验证安装
[root@wyx ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-11/root/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-11/root/usr --mandir=/opt/rh/devtoolset-11/root/usr/share/man --infodir=/opt/rh/devtoolset-11/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-11.2.1-20220127/obj-x86_64-redhat-linux/isl-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.1 20220127 (Red Hat 11.2.1-9) (GCC)
[root@wyx ~]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-11/root/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-11/root/usr --mandir=/opt/rh/devtoolset-11/root/usr/share/man --infodir=/opt/rh/devtoolset-11/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-11.2.1-20220127/obj-x86_64-redhat-linux/isl-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.1 20220127 (Red Hat 11.2.1-9) (GCC)
5、配置环境变量
因为不配置环境变量的话,每打开一个客户端,就需要执行一次上面的第三步配置安装包。
先查看gcc和g++安装位置:
[root@wyx /]# which gcc
/opt/rh/devtoolset-11/root/usr/bin/gcc
[root@wyx /]# which g++
/opt/rh/devtoolset-11/root/usr/bin/g++
编辑文件
vim /etc/profile
在文件后面添加下面两行内容:
PATH=$PATH::/opt/rh/devtoolset-11/root/usr/bin
export PATH
重新加载文件
source /etc/profile