CentOS 7、BigCloud 7?以及Anolis 7等系列OS的默認gcc版本為4.8.5,有時編譯程序或者驅動源碼需要更高版本的gcc,本文以升級至8.3.1版本為例,僅僅需要分別執行下面幾條命令即可,無需手動下載源碼編譯。
分別查看CentOS 7、BigCloud 7以及Anolis 7等系列OS的默認gcc版本,查詢結果如下:
#CentOS?7.9平臺gcc默認版本
[wz@localhost?~]$?cat?/etc/redhat-release?
CentOS?Linux?release?7.9.2009?(Core)
[wz@localhost?~]$?
[wz@localhost?~]$?gcc?-v
使用內建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目標:x86_64-redhat-linux
配置為:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
線程模型:posix
gcc?版本?4.8.5?20150623?(Red?Hat?4.8.5-44)?(GCC)?
#BigCloud?7.6平臺gcc默認版本
[root@localhost?~]#?cat?/etc/redhat-release
BigCloud?Enterprise?Linux?release?7.6.1905?(Core)
[root@localhost?~]#?gcc?-v
Using?built-in?specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target:?x86_64-redhat-linux
Configured?with:?../configure?--prefix=/usr?--mandir=/usr/share/man?--infodir=/usr/share/info?--with-bugurl=http://bugzilla.redhat.com/bugzilla?--enable-bootstrap?--enable-shared?--enable-threads=posix?--enable-checking=release?--with-system-zlib?--enable-__cxa_atexit?--disable-libunwind-exceptions?--enable-gnu-unique-object?--enable-linker-build-id?--with-linker-hash-style=gnu?--enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto?--enable-plugin?--enable-initfini-array?--disable-libgcj?--with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install?--with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install?--enable-gnu-indirect-function?--with-tune=generic?--with-arch_32=x86-64?--build=x86_64-redhat-linux
Thread?model:?posix
gcc?version?4.8.5?20150623?(Red?Hat?4.8.5-44)?(GCC)
#Anolis?7.9平臺gcc默認版本
[root@localhost?~]#?cat?/etc/anolis-release
Anolis?OS?release?7.9
[root@localhost?~]#?gcc?-v
使用內建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/4.8.5/lto-wrapper
目標:aarch64-redhat-linux
配置為:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.openanolis.cn --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-aarch64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-aarch64-redhat-linux/cloog-install --enable-gnu-indirect-function --build=aarch64-redhat-linux
線程模型:posix
gcc?版本?4.8.5?20150623?(Anolis?4.8.5-44.0.1)?(GCC)
網上有很多博客會教我們通過gcc?源碼進行編譯,隨后進行安裝,從而升級到我們想要的版本,但是這種方法小編也進行了嘗試,結果在我的機器上始終編譯報錯,而且根本不知道問題出在哪里,這種方式實施的過程中在不同的平臺及機器可能遇見各種各樣的問題,比較麻煩且容易出錯。
因此,本文我們采用CentOS的一個第三方庫SCL(軟件選集)來進行gcc版本的升級,SCL可以在不覆蓋原系統軟件包的情況下安裝新的軟件包與老軟件包共存并且可以使用scl命令切換,不過也有個缺點就是只支持64位的。
Software collections(SCLs)是一個Linux軟件多版本共存的解決方案,適用于RHEL/CentOS/Fedora。SCL不修改已安裝的軟件版本,也不會與其產生沖突。SCL的創建就是為了給?RHEL/CentOS?用戶提供一種以方便、安全地安裝和使用應用程序和運行時環境的多個(而且可能是更新的)版本的方式,同時避免把系統搞亂。
首先,要解決的第一個問題就是?yum?源的問題。CentOS 7 最晚在2024年6月30后停止更新維護,在此之前在 CentOS 7 可以通過?yum?直接安裝?SCL?源基本都是可以正常使用的。
[root@localhost ~]# rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl-rh/2/3.el7.centos/noarch/centos-release-scl-rh-2-3.el7.centos.noarch.rpm
獲取https://cbs.centos.org/kojifiles/packages/centos-release-scl-rh/2/3.el7.centos/noarch/centos-release-scl-rh-2-3.el7.centos.noarch.rpm
準備中... ################################# [100%]
正在升級/安裝...
1:centos-release-scl-rh-2-3.el7.cen################################# [100%]
[root@localhost ~]# rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl/2/3.el7.centos/noarch/centos-release-scl-2-3.el7.centos.noarch.rpm
獲取https://cbs.centos.org/kojifiles/packages/centos-release-scl/2/3.el7.centos/noarch/centos-release-scl-2-3.el7.centos.noarch.rpm
準備中... ################################# [100%]
正在升級/安裝...
1:centos-release-scl-2-3.el7.centos################################# [100%]
安裝完成后,會默認在?/etc/yum.repos.d?下生成 2 個 repo 源文件:
[root@localhost ~]# ls -l /etc/yum.repos.d/ | grep -i scl
-rw-r--r-- 1 root root 998 12月 11 2018 CentOS-SCLo-scl.repo
-rw-r--r-- 1 root root 971 10月 29 2018 CentOS-SCLo-scl-rh.repo
# CentOS-SCLo-sclo.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
# baseurl=http://mirror.centos.org/centos/7/sclo/$basearch/sclo/
mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-sclo
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-sclo-testing]
name=CentOS-7 - SCLo sclo Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/sclo/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-sclo-source]
name=CentOS-7 - SCLo sclo Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/sclo/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-sclo-debuginfo]
name=CentOS-7 - SCLo sclo Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
# CentOS-SCLo-rh.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
#baseurl=http://mirror.centos.org/centos/7/sclo/$basearch/rh/
mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-rh-testing]
name=CentOS-7 - SCLo rh Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/rh/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-rh-source]
name=CentOS-7 - SCLo rh Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/rh/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-rh-debuginfo]
name=CentOS-7 - SCLo rh Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
隨后,更新 yum 源的緩存:
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache
[root@localhost ~]# yum install centos-release-scl centos-release-scl-rh
devtoolset?本身也有不同的版本,因為我們想要將gcc?升級到8.3.1,所以這里我們安裝的是?devtoolset-8-gcc。
[root@localhost ~]# yum -y install devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
如果想安裝7.x版本的,就改成devtoolset-7-gcc,以此類推:
[root@localhost ~]# yum -y install devtoolset-7-gcc devtoolset-7-gcc-c++ devtoolset-7-binutils
全部文件都會被安裝在/opt/rh/目錄下,如下:
[root@localhost ~]# ls -l /opt/rh/
總用量 0
dr-xr-xr-x 3 root root 32 8月 31 14:23 devtoolset-7
dr-xr-xr-x 3 root root 32 8月 31 14:26 devtoolset-8
另外,我們可以用SCL來管理軟件集,如安裝gcc,首先查看可安裝的版本:
[root@localhost ~]# yum list dev*gcc
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* centos-sclo-rh: mirrors.bfsu.edu.cn
* centos-sclo-sclo: mirrors.bfsu.edu.cn
* epel: mirrors.bfsu.edu.cn
已安裝的軟件包
devtoolset-7-gcc.aarch64 7.3.1-5.16.el7 @centos-sclo-rh
devtoolset-8-gcc.aarch64 8.3.1-3.2.el7 @centos-sclo-rh
可安裝的軟件包
devtoolset-10-gcc.aarch64 10.2.1-11.2.el7 centos-sclo-rh
devtoolset-9-gcc.aarch64 9.3.1-2.2.el7 centos-sclo-rh
隨后,即可用選擇合適的版本安裝。
你可以一次安裝多個版本的devtoolset,需要的時候用下面這條命令切換到對應的版本,激活其運行環境:
[root@localhost ~]# scl enable devtoolset-8 bash
注意:使用exit 退出當前scl版本的bash環境。
大功告成,查看一下gcc版本:
[root@localhost ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/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-8.3.1-20190311/obj-aarch64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
注意:這條激活命令只對本次會話有效,重啟會話后還是會變回原來的
4.8.5版本。scl命令啟用只是臨時的,退出shell或重啟就會恢復原系統gcc版本。
由前面可知,安裝的devtoolset是在?/opt/rh?目錄下的,而其每個版本的目錄下面都有個?enable?文件,該文件是一個ASCII文本文件,如下:
[root@localhost ~]# tree -L 2 /opt/rh/devtoolset-8/
/opt/rh/devtoolset-8/
├── enable
└── root
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib64
├── media
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin -> usr/sbin
├── srv
├── sys
├── tmp
├── usr
└── var
20 directories, 1 file
[root@localhost ~]# file /opt/rh/devtoolset-8/enable
/opt/rh/devtoolset-8/enable: ASCII text
如果需要啟用某個版本,只需要執行如下命令:
[root@localhost devtoolset-8]# source ./enable
緊接著,讓我們在當前會話中,執行gcc -v命令查看其版本,如下:
[root@localhost devtoolset-8]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/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-8.3.1-20190311/obj-aarch64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
根據上面結果可知,gcc版本為8.3.1,?devtoolset已切換成功。所以要想切換到某個X版本,只需要執行:
source /opt/rh/devtoolset-X/enable
可以將對應版本的切換命令寫個shell文件放在配了環境變量的目錄下,需要時隨時切換,或者開機自啟。
當然,也可以配置bashrc或/etc/profile使環境自動生效:
echo "source /opt/rh/devtoolset-8/enable" >> ~/.bashrc
#或
echo "source /opt/rh/devtoolset-8/enable" >> /etc/profile
拓展:舊的gcc是運行的 /usr/bin/gcc,所以也可以將該目錄下的gcc/g++替換為剛安裝的新版本gcc軟連接,免得每次enable。如下:
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++
gcc --version
g++ --version
可能大家用完開發工具集后就會想要刪除它,刪除方法很簡單,輸入以下命令:
[root@localhost ~]# yum remove devtoolset-7*
已加載插件:fastestmirror, langpacks
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 devtoolset-7-binutils.aarch64.0.2.28-11.el7 將被 刪除
---> 軟件包 devtoolset-7-gcc.aarch64.0.7.3.1-5.16.el7 將被 刪除
---> 軟件包 devtoolset-7-gcc-c++.aarch64.0.7.3.1-5.16.el7 將被 刪除
---> 軟件包 devtoolset-7-libstdc++-devel.aarch64.0.7.3.1-5.16.el7 將被 刪除
---> 軟件包 devtoolset-7-runtime.aarch64.0.7.1-4.el7 將被 刪除
--> 解決依賴關系完成
依賴關系解決
=========================================================================================================================================================================================================
Package 架構 版本 源 大小
=========================================================================================================================================================================================================
正在刪除:
devtoolset-7-binutils aarch64 2.28-11.el7 @centos-sclo-rh 24 M
devtoolset-7-gcc aarch64 7.3.1-5.16.el7 @centos-sclo-rh 48 M
devtoolset-7-gcc-c++ aarch64 7.3.1-5.16.el7 @centos-sclo-rh 20 M
devtoolset-7-libstdc++-devel aarch64 7.3.1-5.16.el7 @centos-sclo-rh 17 M
devtoolset-7-runtime aarch64 7.1-4.el7 @centos-sclo-rh 3.2 k
事務概要
=========================================================================================================================================================================================================
移除 5 軟件包
安裝大小:110 M
是否繼續?[y/N]:Y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在刪除 : devtoolset-7-gcc-c++-7.3.1-5.16.el7.aarch64 1/5
正在刪除 : devtoolset-7-libstdc++-devel-7.3.1-5.16.el7.aarch64 2/5
正在刪除 : devtoolset-7-gcc-7.3.1-5.16.el7.aarch64 3/5
正在刪除 : devtoolset-7-binutils-2.28-11.el7.aarch64 4/5
正在刪除 : devtoolset-7-runtime-7.1-4.el7.aarch64 5/5
驗證中 : devtoolset-7-libstdc++-devel-7.3.1-5.16.el7.aarch64 1/5
驗證中 : devtoolset-7-gcc-c++-7.3.1-5.16.el7.aarch64 2/5
驗證中 : devtoolset-7-gcc-7.3.1-5.16.el7.aarch64 3/5
驗證中 : devtoolset-7-runtime-7.1-4.el7.aarch64 4/5
驗證中 : devtoolset-7-binutils-2.28-11.el7.aarch64 5/5
刪除:
devtoolset-7-binutils.aarch64 0:2.28-11.el7 devtoolset-7-gcc.aarch64 0:7.3.1-5.16.el7 devtoolset-7-gcc-c++.aarch64 0:7.3.1-5.16.el7 devtoolset-7-libstdc++-devel.aarch64 0:7.3.1-5.16.el7
devtoolset-7-runtime.aarch64 0:7.1-4.el7
完畢!
不同平臺的編譯環境不一樣,所以?RedHat?就推出了?SCL (Software Collections)?,它可以根據?devtoolset?一起配合來快速統一開發環境,不用一個個的去找各個官網再去編譯源碼安裝。使用?SCL?可以暫時的改變當前用戶的編譯工具,例如你的系統版本?gcc 4.8.5,但是你可以使用?SCL?工具臨時把你的?gcc?版本提升到?8.3.1。
其實,簡單的來說,devtoolset?就是?SCL?提供的一套專門用于?CentOS?或?Red Hat Enterprise Linux?平臺編譯開發的一套工具集。