centos7如何升级GCC编辑器、G++编译器, 安装多个版本及切换
001、系统
[root@localhost home]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core)
002、当前gcc 及 g++版本
[root@localhost home]# gcc --version gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44) Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@localhost home]# g++ --version g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44) Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
003、安装centos-release-scl
yum install centos-release-scl
004、安装devtoolset
yum install devtoolset-8-gcc*
005、激活对应的devtoolset版本
scl enable devtoolset-8 bash
006、查看版本
[root@localhost home]# gcc --version ## 安装成功 gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@localhost home]# g++ --version g++ (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
注意以上只是临时安装成功,重启终端会自动切换为原来的版本。
007、安装其他gcc、g++版本
yum install devtoolset-11-gcc*
008、安装的所有gcc在/opt/rh/目录下:
[root@localhost home]# cd /opt/rh/ [root@localhost rh]# ls devtoolset-11 devtoolset-8
009、不同版本的切换
[root@localhost rh]# gcc --version ## 当前gcc版本 gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@localhost rh]# source /opt/rh/devtoolset-11/enable ## 切换至版本11 [root@localhost rh]# gcc --version ## 切换效果 gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9) Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010、设定某一版本为系统启动的默认版本
mv /usr/bin/gcc /usr/bin/gcc-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/g++ /usr/bin/g++
011、测试设定效果
[root@localhost rh]# reboot ## 重启 [root@localhost rh]# gcc --version ## 开机自动启动为gcc 8版本 gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@localhost rh]# g++ --version g++ (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
参考:https://blog.csdn.net/baidu_39332177/article/details/122846140