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