[c++/gcc] Centos 7.9升级 gcc 4.8.5 到 gcc11 [转]

0 序

  • 本文背景:因在centos 7.9 server上安装nodejs21.7.1,编译nodejs时,依赖了gnu17/gcc11。

例如:遇到 Qt requires c++11 support-std=gnu++17
例如:编译器不支持c++17,就会提示:g++: error: unrecognized command line option ‘-std=c++17’
例如:编译器不支持c++17,就会提示:g++: error: unrecognized command line option ‘-std=gnu++17’

  • 然而,centos 7.9 默认的 gcc版本为 4.8.5。因此,需要升级到 gcc11。这种升级场景,在centos7.9中因为nodejs等重要组件,而变得较为频繁、且重要。故记录之。
  • 环境信息
  • centos : 7.9.2009
  • gcc 原版本 : 4.8.5
  • 目标升级版本 : 11

1 基于centos7.9 安装 gcc 4.8.5(默认版本)

  • 安装
yum -y update gcc
yum -y install gcc+ gcc-c++
  • 查看版本
# g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
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.

2 升级步骤 : gcc 4.8.5 -> gcc 11

  • 安装centos-release-scl

如果出现权限问题使用超级用户(sudo)或者root进行安装

yum -y install centos-release-scl

安装devtoolset,如果想安装其他版本,例如 gcc7.* 改为 devtoolset-7-gcc* 即可

yum -y install devtoolset-11-gcc*

注:启用gcc11,devtoolset会安装在/opt/rh目录下

cd /opt/rh/devtoolset-11/ 
  • 每个版本下面都会有个enable文件,执行以下命令即可启用
source ./enable

如果想要切换到不同的版本时,只要执行命令即可

source /opt/rh/devtoolset-11/enable 
  • 直接替换相应软连接

该方法可以避免每次都要进行enable

mv /usr/bin/gcc /usr/bin/gcc.bak

ln -s /opt/rh/devtoolset-11/root/bin/gcc /usr/bin/gcc

mv /usr/bin/g++ /usr/bin/g++.bak

ln -s /opt/rh/devtoolset-11/root/bin/g++ /usr/bin/g++

mv /usr/bin/c++ /usr/bin/c++.bak

ln -s /opt/rh/devtoolset-11/root/bin/c++ /usr/bin/c++

mv /usr/bin/cpp /usr/bin/cpp.bak

ln -s /opt/rh/devtoolset-11/root/bin/cpp /usr/bin/cpp

mv /usr/bin/cc /usr/bin/cc.bak

ln -s /opt/rh/devtoolset-11/root/bin/cc /usr/bin/cc
  • 查验:gcc版本
# g++ --version
g++ (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
Copyright (C) 2021 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.

# gcc --version
gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
Copyright (C) 2021 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.

3 GCC扩展操作

3.1 查验GCC版本

# g++ --version
g++ (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
Copyright (C) 2021 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.

# gcc --version
gcc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
Copyright (C) 2021 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.

3.2 查看GCC编译器支持的C++版本

  • 检查是否支持C++11
g++ -std=c++11 -E - < /dev/null

这行代码是使用g++编译器来编译一段简单的C++代码,以检查您的系统是否支持C++11。
其中,-std=c++11选项告诉编译器使用C++11标准编译代码。
-E选项告诉编译器只进行预处理,而不进行编译和链接步骤。
最后,- < /dev/null表示将标准输入重定向到/dev/null,以便编译器不必读取任何输入。
如果命令行没有输出(或者只输出了警告信息),则说明您的系统支持C++11。如果输出了错误信息,则说明您的系统不支持C++11。

  • 检查是否支持C++14
g++ -std=c++14 -E - < /dev/null
  • 检查是否支持C++17
g++ -std=c++17 -E - < /dev/null

  • 检查是否支持C++20

注意20要写成2a

g++ -std=c++2a -E - < /dev/null

X 参考文献

posted @ 2024-03-22 21:06  千千寰宇  阅读(590)  评论(0编辑  收藏  举报