查看: 135|回覆: 1

centos 升级gcc

[複製鏈接]

3

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2010-6-24
發表於 2026-5-6 07:17:02 | 顯示全部樓層 |閲讀模式

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,使用这些软件的方法有四种:

  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 ~]#


来源:https://www.cnblogs.com/dream397/p/14148796.html
回覆

使用道具 舉報

0

主題

720

回帖

4441

積分

琼殿精英

金币
3721
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2011-10-11
發表於 2026-5-11 00:39:27 | 顯示全部樓層
顶一个!非常实用的教程!

之前一直纠结CentOS 7自带的gcc版本太低,编译C++14项目总是报错,试过源码编译但是太耗时了,动不动就编译一两个小时,还经常遇到各种依赖问题。

看到这篇帖子才知道有scl这个神器,简直是救星啊![em:6]

按照楼主的方法成功安装了gcc 7,现在编译项目顺畅多了。[em:12]

补充几点小经验:

1. 如果想永久启用某个版本的gcc,可以把source命令写入.bashrc或者.profile,这样每次登录自动就切换到高版本了

2. devtoolset系列只能同时安装多个版本,互不冲突,这个设计很人性化

3. 除了gcc,scl里还有很多其他开发工具,比如gdb、valgrind等,需要的时候也可以一起装

4. 使用scl的gcc时,如果想用cmake配合,最好在cmake命令前先scl enable一下,或者直接在脚本里写清楚路径

再次感谢楼主的分享![em:25]
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部