linux下编译log4cxx

花了一晚上时间编译log4cxx, 总算通过了,这鸟工具,整S我了。

使用log4cxx的版本为0.97 , OS为Fedora 6. 在make的过程中,首先碰到的问题就是:

QUOTE:
../include/log4cxx/xml/domconfigurator.h:249: error: extra qualification 
'log4cxx::xml::DOMConfigurator::' on member 'subst'

以上问题花了近二个小时,才找到答案, 答案见下:
QUOTE:
Index: include/log4cxx/xml/domconfigurator.h 
========================================= 
--- include/log4cxx/xml/domconfigurator.h (revision 384243) 
+++ include/log4cxx/xml/domconfigurator.h (working copy) 
@@ -246,7 +246,7 @@ 
protected: 
static LogString getAttribute(apr_xml_elem*, 
const std::string& attrName); 
- LogString DOMConfigurator::subst(const LogString& value); 
+ LogString subst(const LogString& value); //要在源代码中把以上函数声明改成这句

protected: 
helpers::Properties props;

然后,编译到 /tests/src/util/filter.cpp 时,提示“‘RegEx’ 在此作用域中尚未声明”

进入到filter.cpp文件中,看到该文件使用的是boost库中的regex功能。
QUOTE:
#include 
#include "filter.h"

using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace boost;

String Filter::merge(const String& pattern, const String& in, const String& fmt)
{
USES_CONVERSION;
std::string convPattern = T2A(pattern.c_str());
std::string convIn = T2A(in.c_str());
std::string convFmt = T2A(fmt.c_str());

std::string result = RegEx(convPattern).Merge(convIn, convFmt);
return A2T(result.c_str());

}

首先想到的是: gcc的时候“-lboost_regex”没有添加,在Makfile.in中添加编译选项:"-lboost_regex",还是出现该错误。

最后,查看boost库源代码,发现RegEx函数定义在cregex.hpp中,而不是regex.hpp中,可能是boost库已经更新(把RegEx函数移到cregex.hpp中定义),而log4cxx却还是默认使用旧boost库的原因。

这个错误更改一下头文件编译就OK了。
#include 

以上两个错误,花起来解决的时间都要4个小时。真是无语了

本文来自红联

今天无聊想试试log4cxx的用法,从网上找了个例子,小修改了一下:

#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/helpers/exception.h"

using namespace std;
using namespace log4cxx;
using namespace log4cxx::helpers;

int main(int argc, char* argv[])
{
    LoggerPtr logger(Logger::getLogger("MyApp"));

   try
   {
       BasicConfigurator::configure();
       LOG4CXX_INFO(logger, _T("你说hello"));
       LOG4CXX_DEBUG(logger, "world");
   }
   catch (Exception&)
   {

   }
   return 0;
}

然后写了个makefile:
test:   test_log4cpp1.o
        g++ -o test_log4cpp test_log4cpp1.o
一运行,发现编译通过,但是link出错,找不到log4cxx相关的引用。网上到处搜索资料,基本上都是说要设置LD_LIBRARY_PATH 或者是LIBRARY_PATH,可是我设置了也没有用,还是同样的错误。然后我把makefile中的命令改为:
g++ -L/usr/local/lib -o test_log4cpp test_log4cpp1.o
最后还是一样的错误。。。

在濒临绝望的时候,我去查了一下 gcc的文档,无意间发现一段话,就是-l(小写的L)选项可以用来指定库名。我抱着死马当活马医的心态试了试:
g++ -llog4cxx -o test_log4cpp test_log4cpp1.o
竟然成功了!!真是大喜过望阿!

经过简单的试用发现,log4cxx和log4j还是相当的像的,配置文件基本上可以一模一样,代码方面稍有区别,但总得还是在每个类设置一个static的logger对象,然后用LOG4CXX_LEVEL宏来写log

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值