日期:2014-05-16  浏览次数:20704 次

[转载] log4cxx在Linux下的安装、使用
简单地说,log4cxx就是一个记录日志的C++库(程序运行的时候要保存一些日志到文件,以供将来查看),它是从著名的Java日志库log4j移植而来的,并且它是Apache的一个项目,质量有保证,不用犹豫了,就用它吧!

补一句:Apache声称log4cxx的速度快、灵活性好,但是,速度快是第一位的,灵活性是第二位的("Log4cxx claims to be fast and flexible: speed first, flexibility second."),所以,担心日志记录性能的同学更可以选择log4cxx啦。

但是,这玩意的安装、编译稍微有点麻烦,如果遇到了问题,没耐心的人可能就没兴趣折腾了,我在这里把自己遇到的问题记一下。

系统环境:RHEL 5.3,64位


首先去log4cxx的官方网站下载源码安装包:http://logging.apache.org/log4cxx/,点击左边的“Download”进入下载页面,当前(2011年8月9日)的最新版本是0.10.0,你可以下载 .tar.gz 压缩包,解压出来即可。


然后就可以直接configure,make,make install了吗?不行,因为log4cxx官方提供的源码安装包不是一个all-in-one的包,它还依赖于Apache的另外两个库:Apache Portable Runtime(APR)和Apache Portable Runtime Utility(APR-Util),你可以在这个链接找到它们:http://apr.apache.org/。

(1)安装

安装顺序不能变。首先要安装APR,下载到源码安装包后,解压出来,然后:

./configure –prefix=/usr/local/apr


make


make install


这里把APR安装到了 /usr/local/apr 目录下,注意,千万不要直接./configure,因为那样会把APR的文件安装到若干目录下,非常不利于维护。


文章来源:http://www.codelast.com/

再安装APR-Util,和上面一样,解压出来源码安装包,然后:

./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr

make

make install


这里,通过 –with-apr 参数指定了前一步安装的APR的目录,同时也将APR-Util的安装目录设置在了/usr/local/apr-util 目录下。



最后,就是安装log4cxx了,但是在安装之前,还要再注意一点:configure的时候要指定APR和APR-Util的安装路径:

./configure –prefix=/usr/local/log4cxx –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util

mv libtool libtool.bak

ln -s /usr/bin/libtool libtool

make

make install

注意,中间有两条匪夷所思的命令,如果不执行的话,会报错(我不太记得是在configure的时候还是make的时候报的错了):

/usr/lib/libexpat.so: could not read symbols: File in wrong format


这些问题的解决办法是从网上搜来的,官方的指南做得不给力啊。

文章来源:http://www.codelast.com/

这样就把log4cxx安装上了,然后你需要将log4cxx整合进你的程序中,我是把 log4cxx、APR、APR-Util 的三个静态库文件全部放置到我的工程目录下的某个子目录下,然后在Makefile中指定从这个目录下去寻找库文件。三个静态库名如下:liblog4cxx.a,libapr-1.a,libaprutil-1.a。

此外,我还将 log4cxx、APR、APR-Util 的所有头文件(.h)放置到了工程目录下。并在自己的程序中include工程目录下的这些文件。

然后就是在Makefile中添加上与log4cxx相关的一切东西,包括头文件路径,库文件路径等。如果你编译的时候看到与log4cxx相关的“undefined reference …”的错误,那么肯定是没有找到相关的库文件或头文件,这里需要提醒你的是要添加的几个库文件参数:-llog4cxx,-lapr-1,-laprutil-1。




你把上面的步骤都做好了,这个时候,你再编译你的程序,可能又会遇到以下错误(好吧,的确有点折磨人):

./lib/log4cxx/libaprutil-1.a(apr_xml.o): In function `apr_xml_parser_geterror':

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:478: undefined reference to `XML_ErrorString'

./lib/log4cxx/libaprutil-1.a(apr_xml.o): In function `do_parse':

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:418: undefined reference to `XML_Parse'

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:422: undefined reference to `XML_GetErrorCode'

./lib/log4cxx/libaprutil-1.a(apr_xml.o): In function `cleanup_parser':

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:344: undefined reference to `XML_ParserFree'

./lib/log4cxx/libaprutil-1.a(apr_xml.o): In function `apr_xml_parser_create':

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:381: undefined reference to `XML_ParserCreate'

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:390: undefined reference to `XML_SetUserData'

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:391: undefined reference to `XML_SetElementHandler'

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:392: undefined reference to `XML_SetCharacterDataHandler'

/home/log4cxx/apr-util-1.3.12/xml/apr_xml.c:404: undefined reference to `XML_SetDefaultHandler'

collect2: ld returned 1 exit status

make: *** [cpsAPI] Error 1


文章来源:http://www.codelast.com/

这些乱七八糟的东西是怎么回事?据网上的一些文章说,这是 libaprutil 的一个bug,它不会自动链接到它的依赖项,从而导致了那些错误。要解决这个问题,你在编译的时候添加 -lexpat 参数即可——无论你是在Makefile中,还是在命令行直接用g++命令编译程序,都必须要带上这个参数,否则就会得到上面的那一堆错误(真让人恼火啊)。