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
相关推荐
tntxia1 小时前
linux curl命令详解_curl详解
linux
扛枪的书生4 小时前
Linux 网络管理器用法速查
linux
SkyWalking中文站6 小时前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台
运维·前端·监控
顺风尿一寸7 小时前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
雪梨酱QAQ9 小时前
Kubeneters HA Cluster部署
运维
江华森13 小时前
Spring Cloud 微服务全栈实战:从 Eureka 到 Docker Compose 一文贯通
运维
江华森13 小时前
Matplotlib 数据绘图基础入门
运维
XIAOHEZIcode13 小时前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
江华森13 小时前
NumPy 数值计算基础入门
运维
唐青枫15 小时前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux