最近在linux下使用log4cxx库,按照其官方文档提供的方法来进行编译,不能成功,又利用google搜索了好几个中文博客上讲述在linux下编译使用log4cxx库的方法,依然不能成功,在这里我奉劝写博客或是转载博客指导人的朋友们,首先您起码得自己按照您写的或是转载的编译通过了再发表文章吧,其中有几处明显的低级错误,另外,建议您把你编译时的具体的linux系统及版本附带上,这样才能够成一条完整的信息链,一定要对自己或是转载的文章负责。
下面我把我编译log4cxx过程给大家讲述下:
系统平台
CentOS 6.3 linux内核版本 2.6 g++ (GCC) 4.4.6
以下是详细的编译过程:
一.下载解压
http://logging.apache.org/log4cxx/, http://apr.apache.org/
apr-1.4.6.tar.gz, apr-util-1.4.1.tar.gz, apache-log4cxx-0.10.0.tar.gz
1.tar zxvf apr-1.4.6.tar.gz
2.tar zxvf apr-util-1.4.1.tar.gz
3.tar zxvf apache-log4cxx-0.10.0.tar.gz
二 .编译安装
首先安装apr-1.4.6,切换cd apr-1.4.6,配置./configure –prefix=/usr/local/apr,接着make, make install
接着安装apr-util-1.4.1,切换至cd ../apr-util-1.4.1, ./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr,接着make,make install;
最后安装apache-log4cxx-0.10.0,切换cd ../apache-log4cxx-0.10.0,配置./configure –prefix=/usr/local/log4cxx –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util
注意配置前需进行以下操作:
1.vim src/main/cpp/inputstreamreader.cpp
增加#include <string.h>;
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/inputstreamreader.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/helpers/pool.h>
#include <log4cxx/helpers/bytebuffer.h>
+
#include <string.h>
+
否则会出现inputstreamreader.cpp:66: error: ‘memmove’ was not declared in this scope
make[3]: *** [inputstreamreader.lo] 错误 1
2.vim src/main/cpp/socketoutputstream.cpp
增加#include <string.h>;
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/socketoutputstream.h>
#include <log4cxx/helpers/socket.h>
#include <log4cxx/helpers/bytebuffer.h>
+
#include <string.h>
+
否则会出现socketoutputstream.cpp:52: error: ‘memcpy’ was not declared in this scope
3.vim src/examples/cpp/console.cpp
增加#include <string.h>,#include <stdio.h>;
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <string.h>
+
#include <log4cxx/logger.h>
#include <log4cxx/consoleappender.h>
#include <log4cxx/simplelayout.h>
#include <log4cxx/logmanager.h>
#include <iostream>
#include <locale.h>
否则会出现
console.cpp: In function ‘int main(int, char**)’:
console.cpp:58: 错误:‘puts’在此作用域中尚未声明
三.测试
1.代码:
#include <log4cxx/logger.h>
#include <log4cxx/logstring.h>
#include <log4cxx/propertyconfigurator.h>
int main(int argc, char* argv[])
{
using namespace log4cxx;
// 读取配置文件
PropertyConfigurator::configure(“conf.log”);
// 建立两个logger
LoggerPtr logger1 = Logger::getLogger(“TraceYourMama”);
LoggerPtr logger2 = Logger::getLogger(“Patch”);
LOG4CXX_TRACE(logger1, “跟踪”);
LOG4CXX_WARN(logger1, “警告”);
LOG4CXX_DEBUG(logger1, “调试”);
LOG4CXX_ASSERT(logger1, false, “断言”);
LOG4CXX_FATAL(logger1, “致命”);
LOG4CXX_TRACE(logger2, “跟踪”);
LOG4CXX_ERROR(logger2, “错误”);
return 0;
}
配置文件conf.log的内容
log4j.rootLogger=TRACE, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c – %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=./ZW.log
log4j.appender.logfile.MaxFileSize=100KB
log4j.appender.logfile.MaxBackupIndex=10
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d [%t] %-5p %c – %m%
运行时记得
export LD_LIBRARY_PATH=/usr/local/log4cxx/lib:$LD_LIBRARY_PATH