【C++】CentOS环境搭建-安装log4cplus日志组件包及报错解决方案

本文主要是介绍【C++】CentOS环境搭建-安装log4cplus日志组件包及报错解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

log4cplus简介

log4cplus是C++编写的开源的日志系统,前身是java编写的log4j系统,受Apache Software License保护,作者是Tad E. Smith。

log4cplus具有线程安全、灵活、以及多粒度控制的特点,通过将日志划分优先级使其可以面向程序调试、运行、测试、和维护等全生命周期。你可以选择将日志输出到屏幕、文件、NT event log、甚至是远程服务器;通过指定策略对日志进行定期备份等等。

1.安装必要的依赖项:

sudo yum install -y gcc-c++ make cmake
sudo yum install -y openssl-devel

在这里插入图片描述

2.下载log4cplus源代码

官网下载地址:https://sourceforge.net/projects/log4cplus/files/log4cplus-stable/2.0.7/

github地址:https://github.com/log4cplus/log4cplus

在这里插入图片描述

wget https://github.com/log4cplus/log4cplus/archive/refs/tags/REL_2_0_8.tar.gz
tar -zxvf REL_2_0_8.tar.gz
cd log4cplus-REL_2_0_8/
[root@localhost ~]# wget https://github.com/log4cplus/log4cplus/archive/refs/tags/REL_2_0_8.tar.gz
--2024-05-10 01:54:24--  https://github.com/log4cplus/log4cplus/archive/refs/tags/REL_2_0_8.tar.gz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/log4cplus/log4cplus/tar.gz/refs/tags/REL_2_0_8 [following]
--2024-05-10 01:54:24--  https://codeload.github.com/log4cplus/log4cplus/tar.gz/refs/tags/REL_2_0_8
Resolving codeload.github.com (codeload.github.com)... 20.205.243.165
Connecting to codeload.github.com (codeload.github.com)|20.205.243.165|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/x-gzip]
Saving to: ‘REL_2_0_8.tar.gz’[    <=>                                                                                                                          ] 856,771     1.16MB/s   in 0.7s   2024-05-10 01:54:26 (1.16 MB/s) - ‘REL_2_0_8.tar.gz’ saved [856771][root@localhost ~]# tar -zxvf REL_2_0_8.tar.gz
log4cplus-REL_2_0_8/
log4cplus-REL_2_0_8/.dir-locals.el
log4cplus-REL_2_0_8/.gitattributes
log4cplus-REL_2_0_8/.github/
.....
log4cplus-REL_2_0_8/tests/timeformat_test/
log4cplus-REL_2_0_8/tests/timeformat_test/CMakeLists.txt
log4cplus-REL_2_0_8/tests/timeformat_test/Makefile.am
log4cplus-REL_2_0_8/tests/timeformat_test/expout
log4cplus-REL_2_0_8/tests/timeformat_test/main.cxx
log4cplus-REL_2_0_8/tests/unit_tests.at
log4cplus-REL_2_0_8/tests/unit_tests/
log4cplus-REL_2_0_8/tests/unit_tests/CMakeLists.txt
log4cplus-REL_2_0_8/tests/unit_tests/Makefile.am
log4cplus-REL_2_0_8/tests/unit_tests/unit_tests.cxx
log4cplus-REL_2_0_8/threadpool/
[root@localhost ~]# cd log4cplus-REL_2_0_8/
[root@localhost log4cplus-REL_2_0_8]# 

3.编译和安装log4cplus

./configure
make
make install

效果如下:

bin:/usr/bin:/usr/local/bin:/root/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/root/bin:/usr/local/bin:/root/bin:/sbin" ldconfig -n /usr/local/include/log4cplus/lib
----------------------------------------------------------------------
Libraries have been installed in:/usr/local/include/log4cplus/libIf you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:- add LIBDIR to the 'LD_LIBRARY_PATH' environment variableduring execution- add LIBDIR to the 'LD_RUN_PATH' environment variableduring linking- use the '-Wl,-rpath -Wl,LIBDIR' linker flag- have your system administrator add LIBDIR to '/etc/ld.so.conf'See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------/usr/bin/mkdir -p '/usr/local/include/log4cplus/lib/pkgconfig'/usr/bin/install -c -m 644 log4cplus.pc '/usr/local/include/log4cplus/lib/pkgconfig'
make[3]: Leaving directory `/root/log4cplus-REL_2_0_8'
make[2]: Leaving directory `/root/log4cplus-REL_2_0_8'
make[1]: Leaving directory `/root/log4cplus-REL_2_0_8'
[root@localhost log4cplus-REL_2_0_8]#

OK…成功了

测试:

main.cpp

#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>#include <iostream>using namespace std;
int main(int argc, char **argv)
{using namespace log4cplus;BasicConfigurator config;config.configure();Logger logger = Logger::getInstance("main");LOG4CPLUS_ERROR(logger, "Hello, error!");LOG4CPLUS_WARN(logger, "Hello, warn!");LOG4CPLUS_INFO(logger, "Hello, info!");LOG4CPLUS_DEBUG(logger, "Hello, debug!");return 0;
}

cmakelists.txt

cmake_minimum_required(VERSION 3.28)
project(log4cpuls_eample)
MESSAGE(${PROJECT_SOURCE_DIR})set(CMAKE_CXX_STANDARD 20)add_executable(log4cpuls_eample main.cpp)
target_link_libraries(${PROJECT_NAME} log4cplus)

运行结果:
在这里插入图片描述

附录: 编译报错

1.报错/root/log4cplus-REL_2_0_8/src/filter.cxx:32:10: fatal error: catch.hpp: No such file or directory

#include <catch.hpp>

[root@localhost build]# make
[  1%] Building CXX object src/CMakeFiles/log4cplus.dir/appenderattachableimpl.cxx.o
[  2%] Building CXX object src/CMakeFiles/log4cplus.dir/appender.cxx.o
[  3%] Building CXX object src/CMakeFiles/log4cplus.dir/asyncappender.cxx.o
[  4%] Building CXX object src/CMakeFiles/log4cplus.dir/callbackappender.cxx.o
[  5%] Building CXX object src/CMakeFiles/log4cplus.dir/clogger.cxx.o
[  6%] Building CXX object src/CMakeFiles/log4cplus.dir/configurator.cxx.o
[  7%] Building CXX object src/CMakeFiles/log4cplus.dir/connectorthread.cxx.o
[  9%] Building CXX object src/CMakeFiles/log4cplus.dir/consoleappender.cxx.o
[ 10%] Building CXX object src/CMakeFiles/log4cplus.dir/cygwin-win32.cxx.o
[ 11%] Building CXX object src/CMakeFiles/log4cplus.dir/env.cxx.o
[ 12%] Building CXX object src/CMakeFiles/log4cplus.dir/factory.cxx.o
[ 13%] Building CXX object src/CMakeFiles/log4cplus.dir/fileappender.cxx.o
[ 14%] Building CXX object src/CMakeFiles/log4cplus.dir/fileinfo.cxx.o
[ 15%] Building CXX object src/CMakeFiles/log4cplus.dir/filter.cxx.o
/root/log4cplus-REL_2_0_8/src/filter.cxx:32:10: fatal error: catch.hpp: No such file or directory32 | #include <catch.hpp>|          ^~~~~~~~~~~
compilation terminated.
make[2]: *** [src/CMakeFiles/log4cplus.dir/filter.cxx.o] Error 1
make[1]: *** [src/CMakeFiles/log4cplus.dir/all] Error 2
make: *** [all] Error 2
解决方案:
cat .gitmodules

在这里插入图片描述
然后执行

git clone https://github.com/philsquared/Catch.git
rm -rf catch/ && mv Catch catch

2.报错src/global-init.cxx:44:10: fatal error: ThreadPool.h: No such file or directory #include “ThreadPool.h”

在这里插入图片描述

解决方案:
cat .gitmodules

在这里插入图片描述
然后执行

git clone https://github.com/log4cplus/ThreadPool.git
rm -rf ThreadPool/ && mv ThreadPool threadpool

3.报错:原因为CMAKE版本太低,需要升级CMAKE: 见【C++】CentOS环境搭建-升级CMAKE

[root@localhost log4cplus-REL_2_0_8]# mkdir build && cd build
[root@localhost build]# cmake ..
-- The C compiler identification is GNU 9.3.1
-- The CXX compiler identification is GNU 9.3.1
-- Check for working C compiler: /opt/rh/devtoolset-9/root/usr/bin/cc
-- Check for working C compiler: /opt/rh/devtoolset-9/root/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /opt/rh/devtoolset-9/root/usr/bin/c++
-- Check for working CXX compiler: /opt/rh/devtoolset-9/root/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at CMakeLists.txt:15 (cmake_minimum_required):CMake 3.12 or higher is required.  You are running version 2.8.12.2-- Configuring incomplete, errors occurred!
See also "/root/log4cplus-REL_2_0_8/build/CMakeFiles/CMakeOutput.log".
[root@localhost build]# make
make: *** No targets specified and no makefile found.  Stop.
[root@localhost build]# sudo make install
make: *** No rule to make target `install'.  Stop.
[root@localhost build]# 

4.报错error while loading shared libraries: liblog4cplus-2.0.so.3: cannot open shared object file: No such file or directory

解决方案:
ldconfig /usr/local/lib

这篇关于【C++】CentOS环境搭建-安装log4cplus日志组件包及报错解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/979108

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

SQLite3 在嵌入式C环境中存储音频/视频文件的最优方案

《SQLite3在嵌入式C环境中存储音频/视频文件的最优方案》本文探讨了SQLite3在嵌入式C环境中存储音视频文件的优化方案,推荐采用文件路径存储结合元数据管理,兼顾效率与资源限制,小文件可使用B... 目录SQLite3 在嵌入式C环境中存储音频/视频文件的专业方案一、存储策略选择1. 直接存储 vs

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决