1. 程式人生 > 實用技巧 >centos 升級gcc

centos 升級gcc

CentOS 7官方源的gcc最新版本是4.8.5,釋出於2015年,年代久遠且不支援c++14。要編譯c++14及以上專案,必須要升級現有版本或者安裝高版本的gcc。

解決辦法有兩種:手動編譯(也可以從其他機器拷貝或者網上下載),或從源安裝。

大多數情況下本人都不推薦從原始碼編譯,不僅因為編譯過程中常會出現各種依賴問題需要手動解決,更因為件升級還要再來一次,相當的折騰(單核編譯gcc至少一小時,費時費精力)。

本文介紹從源安裝高版本gcc的辦法。

紅帽其實已經編譯好了高版本的gcc,但未更新到base和epel這兩個常用源中,而是將這些版本放在scl中。

首先安裝scl

yum install -y centos-release-scl

如果你之前用過grouplist/install等命令,應該知道gcc包含在DevelopmentTools這個組中。scl中的gcc/g++軟體包的字首都是devtoolset,包含gcc 6的軟體包是devtoolset-6,其安裝命令是:

yum install -y devtoolset-6-gcc devtoolset-6-gcc-c++

出了gcc 6,scl中還有如下gcc版本:

  • devtoolset-3: gcc 4.9
  • devtoolset-4: gcc 5
  • devtoolset-6: gcc 6
  • devtoolset-7: gcc 7
  • devtoolset-8: gcc 8

至於為什麼沒有devtoolset-5,我也不清楚,估計是包含在devtoolset-4中了吧。

值得說明的是這些軟體包可以同時安裝,不會相互覆蓋和衝突,也不會覆蓋系統的版本。即可以在系統中可同時存在gcc 6, gcc 7, gcc 8等多個版本。

因為不會覆蓋系統預設的gcc,使用這些軟體的方法有四種:

  1. 使用絕對路徑;
  2. 新增可執行檔案路徑到PATH環境變數;
  3. 使用官方推薦的載入命令:scl enable devtoolset-x bash, x為要啟用的版本;
  4. 執行安裝軟體自帶的指令碼:source/opt/rh/devtoolset-x/enable,x為要啟用的版本。

實踐推薦使用最後兩種方式。例如啟用gcc 6:source/opt/rh/devtoolset-6/enable

,接著輸入gcc-v檢視版本已經變成gcc 6.3.1。如果希望長期使用某個高版本,可將此命令寫入.bashrc等配置檔案。

其它

scl以及scl-rh源中的軟體包都安裝在/opt/rh/目錄下,包含可執行檔案、配置等。啟用命令的路徑是/opt/rh/xxx/enable,安裝的服務重啟命令則是systemctl restart rh-xxx,需要加rh或scl字首以區別其他源的包。如果你用過remi/gitlab等源,其行為方式也是類似的。

Instructions

You can get started in three easy steps:

# 1. Install a package with repository for your system:
# On CentOS, install package centos-release-scl available in CentOS repository:
$ sudo yum install centos-release-scl

# On RHEL, enable RHSCL repository for you system:
$ sudo yum-config-manager --enable rhel-server-rhscl-7-rpms

# 2. Install the collection:
$ sudo yum install devtoolset-7

# 3. Start using software collections:
$ scl enable devtoolset-7 bash

At this point you should be able to use gcc and other tools just as a normal application. See examples bellow:

$ gcc hello.c
$ sudo yum install devtoolset-7-valgrind
$ valgrind ./a.out
$ gdb ./a.out

In order to view the individual components included in this collection, including additional development tools, you can run:

$ sudo yum list devtoolset-7\*

Developer Toolset Software Collection available on aarch64 for testing

Based on big demand, there are also aarch64 builds of devtoolset-7 packages available on buildroot (testing repository at this point). To install it on your aarch64 machine, run:

$ sudo yum install centos-release-scl-rh
$ sudo yum-config-manager --enable centos-sclo-rh-testing
$ sudo yum install devtoolset-7

Developer Toolset Software Collections as Docker Formatted Containers

On CentOS 7 and RHEL 7 you can pull the images with the following commands:

$ docker pull registry.access.redhat.com/rhscl/devtoolset-7-perftools-rhel7
$ docker pull registry.access.redhat.com/rhscl/devtoolset-7-toolchain-rhel7
$ docker pull centos/devtoolset-7-perftools-centos7
$ docker pull centos/devtoolset-7-toolchain-centos7

For more on the docker images follow the link to public source repository: https://github.com/sclorg/devtoolset-container

Policy

Community Project: Maintained by upstream communities of developers. The software is cared for, but the developers make no commitments to update the repositories in a timely manner.

gcc 4.8 安裝

[root@DS-VM-Node239 ~]# curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repos.d/hop5.repo [root@DS-VM-Node239 ~]# yum install gcc gcc-g++ -y [root@DS-VM-Node239 ~]# gcc --version gcc (GCC) 4.8.2 20131212 (Red Hat 4.8.2-8) Copyright © 2013 Free Software Foundation, Inc. 本程式是自由軟體;請參看原始碼的版權宣告。本軟體沒有任何擔保; 包括沒有適銷性和某一專用目的下的適用性擔保。 [root@DS-VM-Node239 ~]# g++ --version g++ (GCC) 4.8.2 20131212 (Red Hat 4.8.2-8) Copyright © 2013 Free Software Foundation, Inc. 本程式是自由軟體;請參看原始碼的版權宣告。本軟體沒有任何擔保; 包括沒有適銷性和某一專用目的下的適用性擔保。 [root@DS-VM-Node211 ~]#

gcc 4.9 安裝

[root@DS-VM-Node239 ~]# yum install centos-release-scl -y [root@DS-VM-Node239 ~]# yum install devtoolset-3-toolchain -y [root@DS-VM-Node239 ~]# scl enable devtoolset-3 bash [root@DS-VM-Node239 ~]# gcc --version gcc (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6) Copyright (C) 2014 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@DS-VM-Node239 ~]# g++ --version g++ (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6) Copyright (C) 2014 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@DS-VM-Node239 ~]# gfortran --version GNU Fortran (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6) Copyright (C) 2014 Free Software Foundation, Inc. GNU Fortran comes with NO WARRANTY, to the extent permitted by law. You may redistribute copies of GNU Fortran under the terms of the GNU General Public License. For more information about these matters, see the file named COPYING [root@DS-VM-Node239 ~]#

gcc 5.2 安裝

[root@DS-VM-Node239 ~]# yum install centos-release-scl -y [root@DS-VM-Node239 ~]# yum install devtoolset-4-toolchain -y [root@DS-VM-Node239 ~]# scl enable devtoolset-4 bash [root@DS-VM-Node239 ~]# gcc --version gcc (GCC) 5.2.1 20150902 (Red Hat 5.2.1-2) 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@DS-VM-Node239 ~]# g++ --version g++ (GCC) 5.2.1 20150902 (Red Hat 5.2.1-2) 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@DS-VM-Node239 ~]#