编译安装 gcc8.1.0

01 ubuntu1604desktop_x64 安装gcc8.1.0

系统环境:

gcc版本:gcc version 5.4.0 20160609
build-essential is already the newest version (12.1ubuntu2).
libgmp-dev is already the newest version (2:6.1.0+dfsg-2).
libmpc-dev is already the newest version (1.0.3-1).
libmpfr-dev is already the newest version (3.1.4-1).
The following packages were automatically installed and are no longer required:
  libqpdf17 linux-headers-4.4.0-79 linux-headers-4.4.0-79-generic
  linux-image-4.4.0-79-generic linux-image-extra-4.4.0-79-generic

01.01 系统更新及安装依赖

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential  libgmp-dev libmpfr-dev libmpc-dev

01.02 gcc代码下载及编译

wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-8.1.0/gcc-8.1.0.tar.xz
tar -Jxvf gcc-8.1.0.tar.xz
./gcc-8.1.0/contrib/download_prerequisites
mkdir temp_gcc81 && cd temp_gcc81
../gcc-8.1.0/configure --prefix=/usr/local/gcc-8.1 --enable-threads=posix --disable-checking --disable-multilib
make
sudo make install

01.03 环境配置

设置gcc8.1的安装路径
sudo vim /etc/profile

export PATH="/usr/local/gcc-8.1/bin:$PATH"

01.04 测试

测试文件test_gcc81.cpp

#include <iostream>
int main(int argc,char** argv)
{
    using namespace std;
    cout << __cplusplus << endl;
    return 0;
}

01.05 c++2a

编译 g++ --std=c++2a -o tg81 test_gcc81.cpp
运行结果:

./tg81
201709
gcc8.1.0 demo01

01.06 gcc7.2

gcc7.2 在ubuntu1604_x64下的编译方式完全相同。
测试效果:

$ g++ --std=c++2a -o tg72 test_gcc81.cpp 
g++: error: unrecognized command line option ‘--std=c++2a’; did you mean ‘--std=c++03’?
$ g++ --std=c++14 -o tg72 test_gcc81.cpp 
$ ./tg72 
201402

02 centos7.4x64 上编译安装gcc8.1.0

02.01 系统环境以安装依赖

#默认gcc版本
gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)

# 更新系统
sudo yum update
sudo yum upgrade

# 安装gcc编译依赖
sudo yum install gmp-devel mpfr-devel libmpc-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package 1:gmp-devel-6.0.0-15.el7.x86_64 already installed and latest version
Package mpfr-devel-3.1.1-4.el7.x86_64 already installed and latest version
Package libmpc-devel-1.0.1-3.el7.x86_64 already installed and latest version

02.03 下载源码及编译安装

wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-8.1.0/gcc-8.1.0.tar.xz
tar -Jxvf gcc-8.1.0.tar.xz
./gcc-8.1.0/contrib/download_prerequisites
mkdir temp_gcc81 && cd temp_gcc81
../gcc-8.1.0/configure --prefix=/usr/local/gcc-8.1 --enable-threads=posix --disable-checking --disable-multilib
make
sudo make install

02.03 测试效果:

$cat test_gcc81.cpp
#include <iostream>
int main(int argc,char** argv)
{
    using namespace std;
    cout << __cplusplus << endl;
    return 0;
}

$g++ --std=c++2a -o tg81 test_gcc81.cpp
$./tg81
201709

这里写图片描述

03 centos7.4下yum升级gcc

centos7.4 gcc默认版本是4.8.5,要更新到最新的8.x.x可以用如下方法:
su root切换到root账号下。注意修改/etc/profile内容,需要重启才能生效。

yum install centos-release-scl scl-utils-build
yum install -y devtoolset-8-toolchain
scl enable devtoolset-8 bash
echo "source /opt/rh/devtoolset-8/enable" >> /etc/profile
source /etc/profile
reboot

04 ubuntu18.04 安装多个版本gcc

查看以安装gcc的版本:

gcc -v
ls /usr/bin/gcc*

build-essential包,它包含GCC编译器以及编译软件所需的许多库和其他实用程序。
manpages-dev包是 GNU/Linux 的开发手册。

sudo apt install build-essential manpages-dev

在写本文时,默认的Ubuntu存储库包括几个GCC版本,从5.x.x到8.x.x. 最新版本的GCC是9.3.x,可从Ubuntu Toolchain PPA获得。
ubuntu-toolchain-r/test PPA添加到系统,可加获得GCC的最新版本9.x.x。

sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

按装需要升级或降级的版本。

sudo apt install gcc-8 g++-8 gcc-9 g++-9

update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令、哪个软件版本。其中70,80,90是优先级数值可以自己设定,–slave能保证gcc和g++保持相同的版本。
该命令形式为:
update-alternatives --install+(多版本命令在系统中的进入地址)+(在update-alternatives中的注册名称)+(实际可执行命令的物理地址)+(自动模式下的优先级数字)
需要注意:当切换使用其他版本的gcc时,请务必保持g++的版本和gcc版本的一致性,否则用cmake配置出来的项目遇到c++代码还是会用之前版本的g++。

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7

也可以手动选择gcc/g++优先级:

sudo update-alternatives --config gcc

05 centos7.4 普通用户使用sudo命令

  1. 切换到root账号
    su root
  2. 需改/etc/sudoers文件可写,原来属性是400
    chmod +w /etc/sudoers
  3. L在/etc/sudoers文件中的 root ALL=(ALL) ALL 下面一行增加自己账号的权限,比如自己的账号是 user1,则vim /etc/sudoers
    user1 ALL=(ALL) ALL
    按下 Esc键 ,wq 保存并退出
  4. 修改/etc/sudoers不可写
    chmod -w /etc/sudoers
  5. 再切换到普通用户user1,可以使用sudo 命令。

06 参考:

GCC官网安装文档:https://gcc.gnu.org/wiki/InstallingGCC

07 CentOS 的 Failed to start LSB: Bring up/down networking 问题

CentOS7 在虚拟机中安装时,如果使用多个网卡,可能出现如下错误Failed to start LSB: Bring up/down networking
解决方法是:使用 ip addr 命令查看网卡的mac地址信息

enp0s3 link/ether 08:00:27:17:c8:91
...
enp0s8 link/ether 08:00:27:8b:4b:ba

然后把mac地址信息写入对应网卡的配置文件中
比如: sudo vim /etc/sysconfig/network-scripts/enp0s8
HWADDR="08:00:27:8b:4b:ba"

然后重启系统: sudo reboot

08 ubuntu 添加cmake源

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt-get update

sudo apt-get install cmake
cmake --version

09 centos 编译安装cmake

# yum install -y gcc gcc-c++ make automake
# https://cmake.org/download/
wget https://cmake.org/files/v3.7/cmake-3.7.2.tar.gz
tar -zxvf cmake-3.7.2.tar.gz
cd cmake-3.7.2/
./bootstrap
gmake
gmake install
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值