一 下载与安装
1 在source forge搜索下载 log4cplus***.tar.ga
2 tar xvzf log4cplus-x.x.x.tar.gz //解压
3 cd log4cplus-x.x.x
4 ./configure --prefix=/usr/local/ //检查系统并配置
5 make //编译
6 make install //安装
这样就在/usr/local/下生成了log4cplus的lib和include目录,lib下的.a是静态链接库,.so是动态链接库的静态库,.so.2是动态链接库,相当于windows下的.dll
二 Hello world 测试
1 拷贝官网的测试程序
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
#include <iomanip>
using namespace log4cplus;
int
main()
{
BasicConfigurator config;
config.configure();
Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
LOG4CPLUS_WARN(logger, LOG4CPLUS_TEXT("Hello, World!"));
return 0;
}
2 编译
g++ -DHAVE_SSTREAM -c helloworld.cpp,生成helloworld.o文件,注意,HAVE_SSTREAM这个宏在windows下编译时不用加,具体原因参考log4cplus源码,暂时不清楚。
3 链接
g++ -o helloworld helloworld.o liblog4cplus.a -lrt -lpthread -lrt
-lrt -lpthread -lrt这些参数是linux编译线程的问题,一定要加上。如果lib的路径没有设置过,则使用绝对路径指定或把liblog4cplus.a拷贝至当前目录下。
或编译命令:g++ helloworld.cpp -I /usr/local/include/ -L /usr/local/lib/ -llog4cplus -o helloworld
Note:
- 安装时请转换到超级用户。本人安装由于之前系统时间设置不对,导致时间错误。更改系统时间命令sudo date -s 10/10/2012, sudo /etc/init.d/hwclock.sh restart #将修改后的正确时间写入cmos
- 编译时若找不到liblog4cplus.a库可以使用绝对路径/usr/local/lib/liblog4cplus.a
- 添加动态链接库路径。确保你的Makefile中包含 /usr/local/lib/liblog4cplus.a(静态库)或 -llog4cplus(动态库)即可,头文件在/usr/local/include/log4cplus目录下。对于动态库,要想正常使用,还得将库安装路径加入到
LD_LIBRARY_PATH 中,以管理员身份登录,在/etc/ld.so.conf中加入安装路径,这里是/usr/local/lib,然后执行ldconfig使设置生效即可。