由网络副手--寻路人于2024.09.11 22:36:31发布在服务器运维 Centos 7.9升级 gcc 4.8.5 到 gcc11 阅读194 评论2 喜欢0 本文背景:因在centos 7.9 报错 init_import_site: Failed to import the site module 然而,centos 7.9 默认的 gcc版本为 4.8.5。因此,需要升级到 gcc11。这种升级场景,在centos7.9中因为nodejs等重要组件,而变得较为频繁、且重要。故记录之。 环境信息 centos : 7.9.2009 gcc 原版本 : 4.8.5 目标升级版本 : 11 回到顶部(Back to Top) 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. ``` 回到顶部(Back to Top) 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 ``` 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 ``` 赞 0 分享 赏 您可以选择一种方式赞助本站 支付宝扫码赞助 BraveDu 署名: 网络副手~寻路人
?语言类评语?
字里行间流露出真挚的情感,让人感同身受,共鸣不已。