linux下編譯log4cxx

來源:互聯網
上載者:User

花了一晚上時間編譯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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.