CentOS 7 升级gcc/g++ 至7.3、8.3、9.3

news2024/9/20 10:50:24

功能需求:CentOS 7 编译安装spdlog-1.12.0 提示如下错误信息:

[root@localhost build]# cmake .. && make -j
-- Build spdlog: 1.12.0
-- Build type: Release
-- Generating example(s)
-- Generating install
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/source_code/spdlog-1.12.0/build
Consolidate compiler generated dependencies of target spdlog
[ 80%] Built target spdlog
[ 90%] Building CXX object example/CMakeFiles/example.dir/example.cpp.o
In file included from /usr/local/source_code/spdlog-1.12.0/example/example.cpp:135:0:
/usr/local/source_code/spdlog-1.12.0/include/spdlog/sinks/daily_file_sink.h: 在静态成员函数‘static spdlog::filename_t spdlog::sinks::daily_filename_format_calculator::calc_filename(const filename_t&, const tm&)’中:
/usr/local/source_code/spdlog-1.12.0/include/spdlog/sinks/daily_file_sink.h:57:19: 错误:‘put_time’不是‘std’的成员
         stream << std::put_time(&now_tm, file_path.c_str());
                   ^
/usr/local/source_code/spdlog-1.12.0/example/example.cpp: 在全局域:
/usr/local/source_code/spdlog-1.12.0/example/example.cpp:285:13: 错误:在不同命名空间内对‘template<class T, class Char, class Enable> struct fmt::v9::formatter’的特例化 [-fpermissive]
 struct fmt::formatter<my_type> : fmt::formatter<std::string>
             ^
In file included from /usr/local/source_code/spdlog-1.12.0/include/spdlog/fmt/fmt.h:27:0,
                 from /usr/local/source_code/spdlog-1.12.0/include/spdlog/common.h:50,
                 from /usr/local/source_code/spdlog-1.12.0/include/spdlog/spdlog.h:12,
                 from /usr/local/source_code/spdlog-1.12.0/example/example.cpp:30:
/usr/local/source_code/spdlog-1.12.0/include/spdlog/fmt/bundled/core.h:791:8: 错误:  从‘template<class T, class Char, class Enable> struct fmt::v9::formatter’的定义 [-fpermissive]
 struct formatter {

查看CentOS 7 gcc/g++ 版本信息:

[root@localhost build]# g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

解决办法:升级CentOS 7 gcc/g++ 版本信息。 

GCC版本对C++的支持情况

Cent OS 7 将gcc/g++ 升级为8.3 版本

核心指令代码:

yum -y install centos-release-scl
yum -y install devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
scl enable devtoolset-8 bash

需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
如果要长期使用gcc 8.3的话:

echo "source /opt/rh/devtoolset-8/enable" >>/etc/profile

安装详情

[root@localhost build]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
[root@localhost build]# g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
[root@localhost build]# yum install centos-release-scl-rh
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
base                                                                                                                     | 3.6 kB  00:00:00
docker-ce-stable                                                                                                         | 3.5 kB  00:00:00
extras                                                                                                                   | 2.9 kB  00:00:00
updates                                                                                                                  | 2.9 kB  00:00:00
(1/2): docker-ce-stable/7/x86_64/primary_db                                                                              | 118 kB  00:00:28
(2/2): updates/7/x86_64/primary_db                                                                                       |  24 MB  00:03:19
正在解决依赖关系
--> 正在检查事务
---> 软件包 centos-release-scl-rh.noarch.0.2-3.el7.centos 将被 安装
--> 解决依赖关系完成
*******
[root@localhost build]# yum -y install devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * centos-sclo-rh: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
centos-sclo-rh                                                                                                           | 3.0 kB  00:00:00
centos-sclo-rh/x86_64/primary_db                                                                                         | 3.4 MB  00:00:53
正在解决依赖关系
--> 正在检查事务
---> 软件包 devtoolset-8-binutils.x86_64.0.2.30-55.el7.2 将被 安装
--> 正在处理依赖关系 devtoolset-8-runtime,它被软件包 devtoolset-8-binutils-2.30-55.el7.2.x86_64 需要
---> 软件包 devtoolset-8-gcc.x86_64.0.8.3.1-3.2.el7 将被 安装
---> 软件包 devtoolset-8-gcc-c++.x86_64.0.8.3.1-3.2.el7 将被 安装
--> 正在处理依赖关系 devtoolset-8-libstdc++-devel = 8.3.1-3.2.el7,它被软件包 devtoolset-8-gcc-c++-8.3.1-3.2.el7.x86_64 需要
--> 正在检查事务
---> 软件包 devtoolset-8-libstdc++-devel.x86_64.0.8.3.1-3.2.el7 将被 安装
---> 软件包 devtoolset-8-runtime.x86_64.0.8.1-1.el7 将被 安装
--> 正在处理依赖关系 scl-utils >= 20120927-11,它被软件包 devtoolset-8-runtime-8.1-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 scl-utils.x86_64.0.20130529-19.el7 将被 安装
--> 解决依赖关系完成
******
[root@localhost build]# scl enable devtoolset-8 bash
[root@localhost build]# echo "source /opt/rh/devtoolset-8/enable" >>/etc/profile
[root@localhost build]# 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.

[root@localhost build]# g++ --version
g++ (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.
******

验证C++ 标准函数库std::put_time函数能否正常使用 

前提条件:切换至 /usr/local/source_code/c_demo 目录并创建hello_putTiem.cpp 文件,编辑内容如下:

#include <iostream>
#include "sstream"
#include "ctime"
#include "iomanip"
void  currTime_()
{
    auto t=time(nullptr);
    auto tm=*localtime(&t);

    std::ostringstream oss;
    oss<<std::put_time(&tm,"%d.%m.%Y %H:%M:%S:");
    std::cout<<oss.str()<<std::endl;
}
int main() {
    currTime_();
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

编译并执行:

[root@localhost c_demo]# g++ hello_putTiem.cpp -o hello_putTiem
[root@localhost c_demo]# ll
总用量 36
-rwxr-xr-x. 1 root root  8480 9月  18 17:32 hello_gtk
-rw-r--r--. 1 root root   246 9月  18 17:32 hello_gtk.c
-rwxr-xr-x. 1 root root 15936 11月 22 15:17 hello_putTiem
-rw-r--r--. 1 root root   361 11月 22 15:16 hello_putTiem.cpp
[root@localhost c_demo]# ./hello_putTiem
22.11.2023 15:17:16:
Hello, World!

结论:gcc/g++ 版本升级成功并且支持std:put_time 函数。

再次编译Spdlog-1.12.0

切换至spdlog-1.12.0/build 目录执行如下命令:

[root@localhost spdlog-1.12.0]# cd build/
[root@localhost build]# cmake .. && make -j
-- Build spdlog: 1.12.0
-- Build type: Release
-- Generating example(s)
-- Generating install
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/source_code/spdlog-1.12.0/build
Consolidate compiler generated dependencies of target spdlog
[ 80%] Built target spdlog
[ 90%] Building CXX object example/CMakeFiles/example.dir/example.cpp.o
In file included from /usr/local/source_code/spdlog-1.12.0/example/example.cpp:135:0:
/usr/local/source_code/spdlog-1.12.0/include/spdlog/sinks/daily_file_sink.h: 在静态成员函数‘static spdlog::filename_t spdlog::sinks::daily_filename_format_calculator::calc_filename(const filename_t&, const tm&)’中:
/usr/local/source_code/spdlog-1.12.0/include/spdlog/sinks/daily_file_sink.h:57:19: 错误:‘put_time’不是‘std’的成员
         stream << std::put_time(&now_tm, file_path.c_str());
                   ^
/usr/local/source_code/spdlog-1.12.0/example/example.cpp: 在全局域:
/usr/local/source_code/spdlog-1.12.0/example/example.cpp:285:13: 错误:在不同命名空间内对‘template<class T, class Char, class Enable> struct fmt::v9::formatter’的特例化 [-fpermissive]
 struct fmt::formatter<my_type> : fmt::formatter<std::string>
             ^
In file included from /usr/local/source_code/spdlog-1.12.0/include/spdlog/fmt/fmt.h:27:0,
                 from /usr/local/source_code/spdlog-1.12.0/include/spdlog/common.h:50,
                 from /usr/local/source_code/spdlog-1.12.0/include/spdlog/spdlog.h:12,
                 from /usr/local/source_code/spdlog-1.12.0/example/example.cpp:30:
/usr/local/source_code/spdlog-1.12.0/include/spdlog/fmt/bundled/core.h:791:8: 错误:  从‘template<class T, class Char, class Enable> struct fmt::v9::formatter’的定义 [-fpermissive]
 struct formatter {
        ^
make[2]: *** [example/CMakeFiles/example.dir/example.cpp.o] 错误 1
make[1]: *** [example/CMakeFiles/example.dir/all] 错误 2
make: *** [all] 错误 2

哎,无语了还是无法正常编译,突然想起spdlog 日志开源项目是基于CMake 进行的项目构建,检查一下CMakeLists.txt文件,目录地址:/usr/local/source_code/spdlog-1.12.0/CMakeLists.txt

我这里重点讲解spdlog 编译依赖的cmake 版本信息和g++ 版本信息。

cmake 版本要求:3.10 -3.21 版本
cmake_minimum_required(VERSION 3.10...3.21)

g++  版本信息:

# ---------------------------------------------------------------------------------------
# Compiler config
# ---------------------------------------------------------------------------------------
if(SPDLOG_USE_STD_FORMAT)
    set(CMAKE_CXX_STANDARD 11)  // 默认编译版本是20, 我这里将其修改为11
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
elseif(NOT CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

 再次编译,还是相同的问题。

更多关于spdlog 关于CMake配置请参考:spdlog CMake 构建项目 

spdlog 安装之柳暗花明

通过百度/谷歌,发现其他的C/C++ 开发者是直接引用源码方式。

核心步骤:

1.下载spdlog源码到任意目录下, 我的存放目录地址:/usr/local/source_code/spdlog-1.12.0
2.将spdlog源码的include中的spdlog文件夹整个复制到/usr/include/目录下,即/usr/include/spdlog,这样可以在调用的时候可以找到头文件spdlog相关的内容。

执行如下指令:

[root@localhost include]# cp -r /usr/local/source_code/spdlog-1.12.0/include/spdlog /usr/include/spdlog

3.验证spdlog 日志框架

切换至/usr/local/source_code/spdlog_demo  目录,并创建test_spdlog.cpp文件,文件内容如下:

[root@localhost spdlog_demo]# cat test_spdlog.cpp
//helloSpdlog.cc
#include "spdlog/spdlog.h"

int main()
{
    spdlog::info("Welcome to spdlog!");
    spdlog::error("Some error message with arg: {}", 1);

    spdlog::warn("Easy padding in numbers like {:08d}", 12);
    spdlog::critical("Support for int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);
    spdlog::info("Support for floats {:03.2f}", 1.23456);
    spdlog::info("Positional args are {1} {0}..", "too", "supported");
    spdlog::info("{:<30}", "left aligned");

    spdlog::set_level(spdlog::level::debug); // Set global log level to debug
    spdlog::debug("This message should be displayed..");

    // change log pattern
    spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");

    // Compile time log levels
    // define SPDLOG_ACTIVE_LEVEL to desired level
    SPDLOG_TRACE("Some trace message with param {}", 42);
    SPDLOG_DEBUG("Some debug message");
}

编译并执行:

[root@localhost spdlog_demo]# g++ test_spdlog.cpp -o test_spdlog -std=c++11
[root@localhost spdlog_demo]# ll
总用量 1688
-rwxr-xr-x. 1 root root 1720472 11月 22 16:13 test_spdlog
-rw-r--r--. 1 root root     925 11月 22 15:57 test_spdlog.cpp
[root@localhost spdlog_demo]# ./test_spdlog
[2023-11-22 16:13:45.822] [info] Welcome to spdlog!
[2023-11-22 16:13:45.822] [error] Some error message with arg: 1
[2023-11-22 16:13:45.822] [warning] Easy padding in numbers like 00000012
[2023-11-22 16:13:45.822] [critical] Support for int: 42;  hex: 2a;  oct: 52; bin: 101010
[2023-11-22 16:13:45.822] [info] Support for floats 1.23
[2023-11-22 16:13:45.822] [info] Positional args are supported too..
[2023-11-22 16:13:45.823] [info] left aligned
[2023-11-22 16:13:45.823] [debug] This message should be displayed..

Cent OS 7 将gcc/g++ 升级为7.3\8.3\9.3 版本核心指令

升级到gcc/g++ 7.3:

yum -y install centos-release-scl
yum -y install devtoolset-7-gcc devtoolset-7-gcc-c++ devtoolset-7-binutils
scl enable devtoolset-7 bash

需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
如果要长期使用gcc 7.3的话:

echo "source /opt/rh/devtoolset-7/enable" >>/etc/profile

 升级到gcc/g++ 8.3:

yum -y install centos-release-scl
yum -y install devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
scl enable devtoolset-8 bash

需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
如果要长期使用gcc 8.3的话:

echo "source /opt/rh/devtoolset-8/enable" >>/etc/profile

 升级到gcc/g++ 9.3:

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash

需要注意的是scl命令启用只是临时的,退出shell或重启就会恢复原系统gcc版本。
如果要长期使用gcc 9.3的话:

echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1238609.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

02房价预测

目录 代码 评分算法&#xff1a; 代码 import numpy as np from sklearn import datasets from sklearn.linear_model import LinearRegression# 指定版本才有数据集 # C:\Users\14817\PycharmProjects\pythonProject1\venv\Scripts\activate.bat # pip install scikit-le…

云服务器-从零搭建前后端服务

使用须知 选择0M带宽不能访问公网&#xff08;不分配公网IP&#xff09;&#xff0c;如需分配公网IP请增加带宽值。云服务器ECS默认不开启虚拟内存如您需要使用请登录云服务器内部操作。Linux开启swap&#xff08;虚拟内存&#xff09;、Windows虚拟内存的设置若您购买了数据盘…

2023-11-21 LeetCode每日一题(美化数组的最少删除数)

2023-11-21每日一题 一、题目编号 2216. 美化数组的最少删除数二、题目链接 点击跳转到题目位置 三、题目描述 给你一个下标从 0 开始的整数数组 nums &#xff0c;如果满足下述条件&#xff0c;则认为数组 nums 是一个 美丽数组 &#xff1a; nums.length 为偶数对所有满…

如何使用 Navicat 连接 GaussDB 主备版

Navicat Premium&#xff08;16.2.8 Windows版或以上&#xff09; 已支持对GaussDB 主备版的管理和开发功能。它不仅具备轻松、便捷的可视化数据查看和编辑功能&#xff0c;还提供强大的高阶功能&#xff08;如模型、结构同步、协同合作、数据迁移等&#xff09;&#xff0c;这…

Leetcode——169 多数元素

我的答案 class Solution {public int majorityElement(int[] nums) {int len nums.length;Arrays.sort(nums);int count 1;int res 0;if(len 1){return nums[0];}for(int i0; i<len-1; i){if(nums[i]nums[i1]){count;}else{count 1;}if(count>len/2){res nums[i]…

JavaDS —— 初识集合框架 + 时间/空间复杂度

目录 1. 初识集合框架 1.1 集合框架的初识 1.2 什么是数据结构&#xff1f; 2. 时间与空间复杂度 2.1 时间复杂度 2.2 大O的渐进表示法 2.3 常见时间复杂度计算举例 2.4 空间复杂度 1. 初识集合框架 1.1 集合框架的初识 什么叫集合&#xff1f;什么叫框架&#xff1f;什么又叫集…

用友NC Cloud uploadChunk任意文件上传漏洞复现 [附POC]

文章目录 用友NC Cloud uploadChunk任意文件上传漏洞复现 [附POC]0x01 前言0x02 漏洞描述0x03 影响版本0x04 漏洞环境0x05 漏洞复现1.访问漏洞环境2.构造POC3.复现 0x06 修复建议 用友NC Cloud uploadChunk任意文件上传漏洞复现 [附POC] 0x01 前言 免责声明&#xff1a;请勿利…

敏捷DevOps专家王立杰:端到端DevOps持续交付的5P法则 | IDCF

今天有一个流行的英文缩写词用来刻画这个风云变幻的时代&#xff1a;VUCA&#xff08;乌卡时代&#xff09;。四个英文字母分别表示动荡性&#xff08;Volatility&#xff09;、不确定性&#xff08;Uncertainty&#xff09;、复杂性&#xff08;Complexity&#xff09;和模糊性…

【精选】构建智能木材计数系统:深度学习与OpenCV完美结合(详细教程+源码)

1.研究背景与意义 随着科技的不断发展&#xff0c;计算机视觉技术在各个领域中得到了广泛的应用。其中&#xff0c;卷积神经网络&#xff08;Convolutional Neural Network&#xff0c;CNN&#xff09;作为一种强大的深度学习模型&#xff0c;已经在图像识别、目标检测、人脸识…

C语言--每日五道选择题-- Day22

第一题&#xff08;注意&#xff09; 1.下列 C 代码中&#xff0c;不属于未定义行为的有&#xff1a;______。 A&#xff1a;int i0; i(i); B&#xff1a;char *p"hello"; p[1]E; C&#xff1a;char *p"hello"; char ch*p; D&#xff1a;int i0; printf(&q…

2024年湖北省水利厅的安管人员水安ABC证报考指南

2024年湖北省水利厅的安管人员水安ABC证报考指南 关于水安ABC报考&#xff0c;很多企业乃至个人都不是很清楚流程&#xff0c;叙后尘来给大家科普一下报考详细细节。2023年年度仅剩一次的报考时间定于2023年11月23日至11月28日。在这个时间段才可以网上填写报考资料&#xff0…

JOSEF信号继电器 JX-18A/2 电压 220VAC辅助电源 板后接线

JX-18/2A系列信号继电器 JX-18A/2A1信号继电器&#xff1b; JX-18A/2A2信号继电器&#xff1b; JX-18B /2A1信号继电器; JX-18B/2A2信号继电器; JX-18C/2A1信号继电器; JX-18C/2A2信号继电器; JX-18E/2A1信号继电器; JX-18E/2A2信号继电器; JX-18D/2A1信号继电器; JX…

java继承和重写(代码演示)

java中的继承和重写 概念 继承 在 Java 中&#xff0c;继承是面向对象编程中的重要概念&#xff0c;它允许一个类&#xff08;称为子类&#xff09;继承另一个类&#xff08;称为父类&#xff09;的属性和方法。子类可以继承父类的非私有属性和方法&#xff0c;并且可以添加…

C++一个关于delete的幼稚错误

分析 在项目开发中&#xff0c;遇上这么一个错误&#xff1a; 0xc00000fd stack_overflow 这是一个栈溢出的错误&#xff0c;奇怪&#xff0c;我delete怎么会提示这么一个错误呢&#xff1f; 与是问题了群&#xff1a; 总结 这个错误实在是蠢啊&#xff01; 没有细想&am…

m4s格式视频文件如何转mp4?三个方法教会你!

m4s格式是一种视频分片格式&#xff0c;它将视频文件分成多个小块&#xff0c;方便网络传输和播放。这种格式常用于流媒体服务&#xff0c;如在线视频网站、直播平台等&#xff0c;比如B站哔哩哔哩下载下来的视频就是这种格式。 方法一&#xff1a;野葱视频转换器 一款音视频转…

【UE5】蓝图

在开发过程中&#xff0c;不可避免地会有一些蓝图的操作。同时&#xff0c;蓝图也是UE很有特色的一个内容。 了解蓝图是什么&#xff08;What is BP?&#xff09; 广义上&#xff1a;蓝图是UE内置的脚本语言&#xff0c;可以书写类似代码的逻辑。&#xff08;任何可以连那种块…

代码随想录算法训练营第四十三天【动态规划part05】 | 1049. 最后一块石头的重量 II、494. 目标和、474.一和零

1049. 最后一块石头的重量 II 题目链接&#xff1a; 力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 求解思路&#xff1a; 等于把石头尽量分成重量相同的两堆 动规五部曲 确定dp数组及其下标含义&#xff1a;容量为j的背包&#xff0c;最多能装…

各大电商平台双十一“狂飙”,如何选择商城系统?

今年是“双十一”的第十五年。作为各大平台和品牌的全年最重要的营销节点&#xff0c;品牌们可谓是来势汹汹&#xff0c;各种促销活动和优惠力度让人眼花缭乱。 淘天数据显示&#xff0c;天猫促销活动开售当晚&#xff0c;155个品牌开卖成交额突破1亿元&#xff1b;首小时内7.1…

前端数组方法汇总集锦

前言 数组主要使用场景有&#xff1a; 存储和处理数据&#xff1a;数组是一种有序的数据结构&#xff0c;可以用来存储和处理多个相关的数据。在前端开发中&#xff0c;我们经常使用数组来存储和处理列表、表格、选项等数据。 循环和遍历&#xff1a;数组提供了循环和遍历的功能…

three.js学习总结超详细附带素材及源码

three.js学习总结超详细附带素材及源码 安装three.js npm install --save three引入three.js import * as THREE from threethree.js结构 three.js坐标 创建一个场景 scene场景&#xff0c;camera相机&#xff0c;renderer渲染器 创建一个场景 this.scene new THREE.Scene(…