当前位置:   article > 正文

【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
  • 1
  • 2

在这里插入图片描述

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/
  • 1
  • 2
  • 3
[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]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

3.编译和安装log4cplus

./configure
make
make install
  • 1
  • 2
  • 3

效果如下:

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/lib

If 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 variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during 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]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

附录: 编译报错

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 directory
   32 | #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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
解决方案:
cat .gitmodules
  • 1

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

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

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

在这里插入图片描述

解决方案:
cat .gitmodules
  • 1

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

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

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]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/576831
推荐阅读
相关标签
  

闽ICP备14008679号