【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

相关文章

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

在MySQL执行UPDATE语句时遇到的错误1175的解决方案

《在MySQL执行UPDATE语句时遇到的错误1175的解决方案》MySQL安全更新模式(SafeUpdateMode)限制了UPDATE和DELETE操作,要求使用WHERE子句时必须基于主键或索引... mysql 中遇到的 Error Code: 1175 是由于启用了 安全更新模式(Safe Upd

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Java下载文件中文文件名乱码的解决方案(文件名包含很多%)

《Java下载文件中文文件名乱码的解决方案(文件名包含很多%)》Java下载文件时,文件名中文乱码问题通常是由于编码不正确导致的,使用`URLEncoder.encode(filepath,UTF-8... 目录Java下载文件中文文件名乱码问题一般情况下,大家都是这样为了解决这个问题最终解决总结Java下

Spring Boot整合log4j2日志配置的详细教程

《SpringBoot整合log4j2日志配置的详细教程》:本文主要介绍SpringBoot项目中整合Log4j2日志框架的步骤和配置,包括常用日志框架的比较、配置参数介绍、Log4j2配置详解... 目录前言一、常用日志框架二、配置参数介绍1. 日志级别2. 输出形式3. 日志格式3.1 PatternL

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

MySQL8.2.0安装教程分享

《MySQL8.2.0安装教程分享》这篇文章详细介绍了如何在Windows系统上安装MySQL数据库软件,包括下载、安装、配置和设置环境变量的步骤... 目录mysql的安装图文1.python访问网址2javascript.点击3.进入Downloads向下滑动4.选择Community Server5.

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.