Glog在linux和windows下的编译安装和使用

Glog是google推出的一个log库,可以运行在linux和windows下。对于需要跨平台运行的C++程序使用glog作为log库是一个不错的选择。这里简单介绍下glog在linux和windows下编译和使用

1 linux编译

Download https://github.com/google/glog

Unzip glog-master.zip

Then cd glog-master directory

./autogen.sh && ./configure && make && sudo make install

 

The lib will be installed at /usr/local/lib/libglog.so

/usr/local/include/glog

 

2 windows 编译

在glog目录下创建cmake_build目录,

启动powershell进入cmake_build目录,运行:

cmake -G "Visual Studio 15 2017" -DBUILD_SHARED_LIBS=yes -DBUILD_TESTING=no -T host=x64 -A x64  ..

 

命令结束后,会在cmake_build目录下产生glog.sln工程文件

使用Visual Studio 2017打开glog.sln工程文件,编译如下所示:

Glog在linux和windows下的编译安装和使用

编译完成后,库文件位于cmake_build\Release目录:

Glog在linux和windows下的编译安装和使用

3 usage

The manual document is at glog-master/doc.

Run “firefox ./glog.html” will open the document.

一个简单的例子如下:

#include <glog/logging.h>

FLAGS_log_dir = "./../../delivery/bin";

FLAGS_stderrthreshold = 0;

google::InitGoogleLogging(“name”);

 

// keep your logs for 7 days

google::EnableLogCleaner(7);

LOG(INFO) << "Constructor starting, configure file is " << argv[1];