【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

相关文章

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri