赞
踩
功能需求: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++ 版本信息。
核心指令代码:
- 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.
- ******
前提条件:切换至 /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/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 构建项目
通过百度/谷歌,发现其他的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..
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
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
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。