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包含在Development Tools
这个组中。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,使用这些软件的方法有四种:
- 使用绝对路径;
- 添加可执行文件路径到PATH环境变量;
- 使用官方推荐的加载命令:
scl enable devtoolset-x bash
, x为要启用的版本; - 执行安装软件自带的脚本:
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 ~] # |