【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

相关文章

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re

Jsoncpp的安装与使用方式

《Jsoncpp的安装与使用方式》JsonCpp是一个用于解析和生成JSON数据的C++库,它支持解析JSON文件或字符串到C++对象,以及将C++对象序列化回JSON格式,安装JsonCpp可以通过... 目录安装jsoncppJsoncpp的使用Value类构造函数检测保存的数据类型提取数据对json数

深入理解C++ 空类大小

《深入理解C++空类大小》本文主要介绍了C++空类大小,规定空类大小为1字节,主要是为了保证对象的唯一性和可区分性,满足数组元素地址连续的要求,下面就来了解一下... 目录1. 保证对象的唯一性和可区分性2. 满足数组元素地址连续的要求3. 与C++的对象模型和内存管理机制相适配查看类对象内存在C++中,规

mac安装redis全过程

《mac安装redis全过程》文章内容主要介绍了如何从官网下载指定版本的Redis,以及如何在自定义目录下安装和启动Redis,还提到了如何修改Redis的密码和配置文件,以及使用RedisInsig... 目录MAC安装Redis安装启动redis 配置redis 常用命令总结mac安装redis官网下

python 字典d[k]中key不存在的解决方案

《python字典d[k]中key不存在的解决方案》本文主要介绍了在Python中处理字典键不存在时获取默认值的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录defaultdict:处理找不到的键的一个选择特殊方法__missing__有时候为了方便起见,

Mycat搭建分库分表方式

《Mycat搭建分库分表方式》文章介绍了如何使用分库分表架构来解决单表数据量过大带来的性能和存储容量限制的问题,通过在一对主从复制节点上配置数据源,并使用分片算法将数据分配到不同的数据库表中,可以有效... 目录分库分表解决的问题分库分表架构添加数据验证结果 总结分库分表解决的问题单表数据量过大带来的性能

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

如何安装 Ubuntu 24.04 LTS 桌面版或服务器? Ubuntu安装指南

《如何安装Ubuntu24.04LTS桌面版或服务器?Ubuntu安装指南》对于我们程序员来说,有一个好用的操作系统、好的编程环境也是很重要,如何安装Ubuntu24.04LTS桌面... Ubuntu 24.04 LTS,代号 Noble NumBAT,于 2024 年 4 月 25 日正式发布,引入了众

如何安装HWE内核? Ubuntu安装hwe内核解决硬件太新的问题

《如何安装HWE内核?Ubuntu安装hwe内核解决硬件太新的问题》今天的主角就是hwe内核(hardwareenablementkernel),一般安装的Ubuntu都是初始内核,不能很好地支... 对于追求系统稳定性,又想充分利用最新硬件特性的 Ubuntu 用户来说,HWEXBQgUbdlna(Har

Python基于火山引擎豆包大模型搭建QQ机器人详细教程(2024年最新)

《Python基于火山引擎豆包大模型搭建QQ机器人详细教程(2024年最新)》:本文主要介绍Python基于火山引擎豆包大模型搭建QQ机器人详细的相关资料,包括开通模型、配置APIKEY鉴权和SD... 目录豆包大模型概述开通模型付费安装 SDK 环境配置 API KEY 鉴权Ark 模型接口Prompt