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
相关推荐
福建佰胜张工1 小时前
深度解析 Allen-Bradley 150-B135NBDA 智能电机控制器:选型、参数与工业应用
运维·数据库·单片机·嵌入式硬件·安全·自动化
wefg11 小时前
【Docker】镜像相关指令
运维·docker·容器
feng_you_ying_li1 小时前
linux之网络层
linux·运维·网络
未来之窗软件服务2 小时前
Linux 运维日志删除的多种方式—东方仙盟
linux·运维·服务器·仙盟创梦ide·东方仙盟
卓豪终端管理2 小时前
IT 运维视角下的终端数据安全:能力框架与落地清单
运维
小雉系统2 小时前
BcoreOS实战:用一个升级包,让Rocky Linux跑进Debian服务器
linux·运维·服务器·debian
thethefighter2 小时前
快速预览Word、WPS秒开,支持空格弹窗
linux·word·wps·快速预览·空格弹窗·nhdeepview
甜到心里的蛋糕2 小时前
从 QQ 机器人到 B 站 AI 运营:一套可复现的视频流水线踩坑实录
运维·人工智能·python·自动化·视频制作
QYR-分析2 小时前
26.2% 高增!场内及园区无人物流车,厂内短驳自动化的 L4 货运载体
运维·自动化
用户8351414049492 小时前
把日志和寄存器值喂给AI做故障定位,我的实际问法
linux·ubuntu