install GCC on CentOS 7

1. Compile and Install Latest Version

The default GCC that comes with the CentOS 7.2 is GCC 4.8.5, which does not support the complete C++11 standard, for example, it does not fully support regular expressions. In order to use regular expression functions, we need to install at least GCC 4.9.0. The following installation procedure is applicable to CentOS 7 and are not tested on other Linux systems. Also you have to make sure that you have root privelege.
Update:
GCC 8.3 has been released on Feb 22, 2019. The installation process is the same as prvevious versions. Download the corrent tar file from the GNU ftp server, compile and install it.

downloading GCC source code
You can download the GCC source code from the official GNU ftp.

bash 复制代码
​
curl https://ftp.gnu.org/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.bz2 -O
tar jxvf gcc-5.4.0.tar.bz2
​

Install dependencies
We need to install 3 dependencies packages.
It is recommended to install these packages through yum.

bash 复制代码
yum install gmp-devel mpfr-devel libmpc-devel

Configuration and install
Unlike other packages, it is recommended to create another build directory outside of the GCC source directory to build GCC.

bash 复制代码
mkdir gcc-5.4.0-build
cd gcc-5.4.0-build
../gcc-5.4.0/configure --enable-languages=c,c++ --disable-multilib
make -j$(nproc) && make install

The comiplation process may take a long time and you need to be patient. It will install GCC under /usr/local. You can change the install dir using --prefix option if you prefer.

Post-installation
You should add the install dir of GCC to your PATH and LD_LIBRARY_PATH in order to use the newer GCC. Add the following settings to /etc/profile:

bash 复制代码
export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH

Maybe a restart of your current session is also needed.
You can download the whole install script here.

2. yum install

2.1 Development Tools

================================================================
CentOS默认的开发工具包集合
包含老版本的GCC、gcc-c++、make、binuitls、autoconf、automake、libtool等
提供最基本的编译能力

The default CentOS repositories contain a package group named Development Tools that contains the GCC compiler and a lot of libraries and other utilities required for compiling software.
To install the Development Tools including the GCC Compiler run:

bash 复制代码
sudo yum groupinstall "Development Tools"   # 默认版本,不是最新,确保所有基础开发依赖都已安装

The command will install a bunch of new packages including gcc, g++ and make.
You may also want to install the manual pages about using GNU/Linux for development:

bash 复制代码
sudo yum install man-pages

Validate that the GCC compiler is successfully installed by using the gcc --versioncommand which will print the GCC version:

bash 复制代码
gcc --version

The default version of GCC available in the CentOS 7 repositories is 4.8.5:

bash 复制代码
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
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.

That's it. GCC is now installed on your system and you can start using it.

2.2 SCL(Software Collections)-- install multiple versions of GCC on CentOS 7

Software Collections , also known as SCL is a community project that allows you to build, install, and use multiple versions of software on the same system, without affecting system default packages . By enabling Software Collections you will gain access to the newer versions of programming languages and services which are not available in the core repositories.
The SCL repositories provide a package named Developer Toolset which includes newer versions of the GNU Compiler Collection, and other development and debugging tools.
(1)First, install the CentOS SCL release file .
It is part of the CentOS extras repository and can be installed by running the following command:

bash 复制代码
sudo yum install centos-release-scl   # 安装 scl 的 yum 源

ls /etc/yum.repos.d/                  # 可看到增加了两个yum源:CentOS-SCLo-scl.repo   CentOS-SCLo-scl-rh.repo

(2)Currently, the following Developer Toolset collections are available:

  • Developer Toolset 8 (20190801 yum search devtoolset-8 查询可用,20210408 可用9 )
  • Developer Toolset 7
  • Developer Toolset 6

===========================================
devtoolset-8(新版开发工具集)
性质:Red Hat Software Collections (SCL) 提供的新版工具链
包含内容:GCC 8.3.1(支持 C11/C++14)、GDB 8.2、其他新版工具
版本:比系统默认新很多
作用:提供现代编译器,支持新特性(如 C11 原子操作)
不完全替代 Development Tools,它们是互补关系,通常 devtoolset 足够用, 不需要额外安装 Development Tools,除非有兼容性问题

In this example we will install the Developer Toolset version 8. To do so type the following command on your CentOS 7 terminal:

bash 复制代码
sudo yum install devtoolset-8     # 安装某版本开发工具集(可安装多个版本)

To access GCC version 8 you need to launch a new shell instance using the Software Collection scl tool:

bash 复制代码
scl enable devtoolset-8 bash      # 新启一个shell环境,使用指定版本工具集

Now if you check the GCC version, you'll notice that GCC 8 is the default version in your current shell:

bash 复制代码
gcc --version
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Copyright (C) 2018 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.

At this point, you can use the newer GCC version just as any other normal tool.

2.3 对于 CentOS 6,如果 SCL 的 yum 源失效,使用阿里云镜像源

备份原来的 SCL 源

bash 复制代码
sudo mv /etc/yum.repos.d/CentOS-SCLo-scl.repo /etc/yum.repos.d/CentOS-SCLo-scl.repo.backup

sudo mv /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo.backup

创建阿里云 SCL 源配置

bash 复制代码
​
sudo tee /etc/yum.repos.d/CentOS-SCLo-scl.repo << 'EOF'
[centos-sclo-sclo]
name=CentOS-6 - SCLo sclo - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos-vault/6.10/sclo/$basearch/sclo/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
[centos-sclo-rh]
name=CentOS-6 - SCLo rh - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos-vault/6.10/sclo/$basearch/rh/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6
EOF

​

清理缓存

bash 复制代码
sudo yum clean all
sudo yum makecache

安装 devtoolset-8

bash 复制代码
sudo yum install devtoolset-8 -y
sudo yum install devtoolset-8 --nogpgcheck -y   # 安装时禁用gpg检查

临时使用新版GCC

bash 复制代码
scl enable devtoolset-8 bash

永久使用新版GCC

bash 复制代码
echo 'source /opt/rh/devtoolset-8/enable' >> ~/.bashrc    # 将新版GCC加入PATH
source ~/.bashrc
相关推荐
东城居士10 分钟前
理解设备树下的platform驱动编写
linux·嵌入式系统
小义_14 分钟前
Linux 补充
linux·服务器
晴天的雨.99216 分钟前
【Linux】Shell 命令以及运行原理+权限管理
linux·运维·服务器
djjjx.21 分钟前
Linux工具篇 (六):GDB 调试从底层逻辑到高阶实战
linux·运维·服务器·gdb
IT大白鼠24 分钟前
彭大帅的AI运维助手实战案例 5 · 新接手的服务器,先让 AI 摸底
运维·服务器·人工智能
coward9142 分钟前
RK3576 双千兆 GMAC 驱动加载异常问题排查与总结
linux
幼儿园蛋小黄1 小时前
从设备树到 ARM 驱动:嵌入式 Linux 驱动开发学习总结
linux·arm开发·驱动开发
dishugj1 小时前
HANA集群计划内切换与维护操作
java·linux·数据库
该逃避避1 小时前
出海广告跑不起来怎么办?动态住宅IP助力广告跑量
运维·服务器·网络
一条泥憨鱼1 小时前
【从0开始学习计算机网络】| ip协议-ttl-标识-分片偏移-ip分片与重组
运维·服务器·网络