很多場景我們編譯C源碼,都需要使用gcc4.8及以上版本,比如編譯MySQL 8.0、GRPC等,原因是需要支持C++11
。但CentOS 6
其內置版本是gcc4.4。
使用
gcc --version
可以查看版本。
$gcc --version
gcc version 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
源碼編譯
常規升級辦法是:
1、下載gcc新版本;
2、編譯。
但gcc編譯起來真的是非常非常的慢,而且特別耗資源,編譯到后面選擇放棄了(ps:編譯的機器配置不高)。后來發現了devtoolset
這個工具,幾分鍾就搞定了gcc版本升級的問題。
使用scl+devtoolset
使用devtoolset
后gcc的升級流程是:
1、安裝SCL
源;
2、安裝devtoolset
工具集;
3、使用scl
命令啟用工具集。
注:建議安裝
devtoolset-6
(devtoolset-6目前gcc版本為6.3)及以上版本,因為devtoolset-4
及之前的版本都已經結束支持,只能通過增加源方法安裝。
升級到gcc 8
yum -y install centos-release-scl
yum -y install devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
scl enable devtoolset-8 bash
下面是安裝過程的部分輸出:
Installing:
devtoolset-8-binutils x86_64 2.30-55.el6.1 centos-sclo-rh 6.0 M
devtoolset-8-gcc x86_64 8.3.1-3.1.el6 centos-sclo-rh 32 M
devtoolset-8-gcc-c++ x86_64 8.3.1-3.1.el6 centos-sclo-rh 12 M
Installing for dependencies:
devtoolset-8-libstdc++-devel x86_64 8.3.1-3.1.el6 centos-sclo-rh 2.9 M
devtoolset-8-runtime x86_64 8.1-1.el6 centos-sclo-rh 1.0 M
Transaction Summary
================================================================================
Install 5 Package(s)
Total download size: 54 M
Installed size: 141 M
查看版本:
$ gcc --version
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
需要注意的是scl
命令啟用只是臨時的,退出shell或重啟就會恢復原系統gcc版本。如果要長期使用gcc 8
的話:
echo "source /opt/rh/devtoolset-8/enable" >>/etc/profile
這樣退出shell重新打開就是新版的gcc
了。其它版本同理。
上面安裝devtoolset
的時候指定僅安裝gcc
、gcc-c++
、binutils
,如果僅寫yum -y install devtoolset-8
則會下載所有相關工具,會包含:
Installing:
devtoolset-8 x86_64 8.1-1.el6 centos-sclo-rh 5.4 k
Installing for dependencies:
avahi-libs x86_64 0.6.25-17.el6 base 55 k
devtoolset-8-dwz x86_64 0.12-1.1.el6 centos-sclo-rh 95 k
devtoolset-8-dyninst x86_64 9.3.2-6.el6 centos-sclo-rh 3.5 M
devtoolset-8-elfutils x86_64 0.176-1.el6 centos-sclo-rh 406 k
devtoolset-8-elfutils-libelf x86_64 0.176-1.el6 centos-sclo-rh 200 k
devtoolset-8-elfutils-libs x86_64 0.176-1.el6 centos-sclo-rh 297 k
devtoolset-8-gcc-gfortran x86_64 8.3.1-3.1.el6 centos-sclo-rh 12 M
devtoolset-8-gdb x86_64 8.2-3.el6 centos-sclo-rh 3.3 M
devtoolset-8-libquadmath-devel x86_64 8.3.1-3.1.el6 centos-sclo-rh 156 k
devtoolset-8-ltrace x86_64 0.7.91-1.el6 centos-sclo-rh 133 k
devtoolset-8-make x86_64 1:4.2.1-4.el6 centos-sclo-rh 484 k
devtoolset-8-memstomp x86_64 0.1.5-5.el6 centos-sclo-rh 340 k
devtoolset-8-oprofile x86_64 1.3.0-2.el6 centos-sclo-rh 2.7 M
devtoolset-8-perftools x86_64 8.1-1.el6 centos-sclo-rh 2.8 k
devtoolset-8-strace x86_64 4.24-2.el6 centos-sclo-rh 988 k
devtoolset-8-systemtap x86_64 3.3-1.el6 centos-sclo-rh 144 k
devtoolset-8-systemtap-client x86_64 3.3-1.el6 centos-sclo-rh 2.8 M
devtoolset-8-systemtap-devel x86_64 3.3-1.el6 centos-sclo-rh 2.2 M
devtoolset-8-systemtap-runtime x86_64 3.3-1.el6 centos-sclo-rh 406 k
devtoolset-8-toolchain x86_64 8.1-1.el6 centos-sclo-rh 3.0 k
devtoolset-8-valgrind x86_64 1:3.14.0-16.el6 centos-sclo-rh 11 M
json-c x86_64 0.11-13.el6 base 27 k
kernel-debug-devel x86_64 2.6.32-754.28.1.el6 updates 11 M
libgfortran5 x86_64 8.2.1-1.3.1.el6_10 updates 678 k
libquadmath x86_64 8.2.1-1.3.1.el6_10 updates 160 k
zip x86_64 3.0-1.el6_7.1 base 259 k
Transaction Summary
================================================================================
Install 27 Package(s)
Total download size: 53 M
Installed size: 152 M
升級到gcc 7.3
yum -y install centos-release-scl
yum -y install devtoolset-7-gcc devtoolset-7-gcc-c++ devtoolset-7-binutils
scl enable devtoolset-7 bash
升級到gcc 6.3
yum -y install centos-release-scl
# install gcc gcc-c++
yum -y install devtoolset-6-gcc devtoolset-6-gcc-c++ devtoolset-6-binutils
scl enable devtoolset-6 bash
升級到gcc 5.2
wget https://copr.fedoraproject.org/coprs/hhorak/devtoolset-4-rebuild-bootstrap/repo/epel-6/hhorak-devtoolset-4-rebuild-bootstrap-epel-6.repo -O /etc/yum.repos.d/devtoolset-4.repo
yum install devtoolset-4-gcc devtoolset-4-gcc-c++ devtoolset-4-binutils -y
scl enable devtoolset-4 bash
已經停止支持的
devtoolset4
及之前版本的安裝方法,可能比較慢。
升級到gcc 4.9
wget https://copr.fedoraproject.org/coprs/rhscl/devtoolset-3/repo/epel-6/rhscl-devtoolset-3-epel-6.repo -O /etc/yum.repos.d/devtoolset-3.repo
yum -y install devtoolset-3-gcc devtoolset-3-gcc-c++ devtoolset-3-binutils
scl enable devtoolset-3 bash
升級到gcc 4.8
wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtoolset-2.repo
yum -y install devtoolset-2-gcc devtoolset-2-gcc-c++ devtoolset-2-binutils
scl enable devtoolset-2 bash
參考
1、為CentOS 6、7升級gcc至4.8、4.9、5.2、6.3、7.3等高版本
http://www.vpser.net/manage/centos-6-upgrade-gcc.html
2、CentOS 下安裝高版本 GCC - 運維之美
https://www.hi-linux.com/posts/25767.html
3、Developer Toolset 8 — Software Collections
https://www.softwarecollections.org/en/scls/rhscl/devtoolset-8/
4、Product Documentation for Red Hat Developer Toolset 9 - Red Hat Customer Portal
https://access.redhat.com/documentation/en-us/Red_Hat_Developer_Toolset/9/