【问题标题】:Configure google glog and gflags for c++为 c++ 配置 google glog 和 gflags
【发布时间】:2014-06-28 04:44:35
【问题描述】:

我一直在尝试为我的 C++ 应用程序配置 google 日志库 glog,但我找不到任何有关如何使其实际工作的信息,而且错误消息也没有什么帮助。

这是我尝试执行的示例代码,我正在执行./myapp --v=2,但我得到“错误:未知命令行标志'v'”。该库是否有任何文档,或者有人知道如何正确配置它吗?

#include <glog/logging.h>
#include <gflags/gflags.h>

int main(int argc, char** argv) {
    google::InitGoogleLogging(argv[0]);
    google::ParseCommandLineFlags(&argc, &argv, true);

    VLOG(1) << "I'm printed when you run the program with --v=1 or higher";
    VLOG(2) << "I'm printed when you run the program with --v=2 or higher";
    return 0;
}

【问题讨论】:

  • google-glog.googlecode.com/svn/trunk/doc/glog.html 的文档没有提及您对google::ParseCommandLineFlags(&amp;argc, &amp;argv, true); 的调用...如果您不包含该内容会怎样?
  • 如果我不包含该代码,则不会执行命令行解析并且不会启用日志记录。 google-glog“文档”引用了提到该调用的 gflags“文档”
  • 另外,最好先调用ParseCommandLineFlags,因为任何glog 特定标志在解析命令行标志之前都不会生效(例如--logtostderr=true)。

标签: c++ logging glog


【解决方案1】:

GLog 需要在“google”命名空间而不是现在默认的“gflags”命名空间中编译的 GFlags。

要设置此命名空间,您必须从源代码编译和安装 gflags,并将 GFLAGS_NAMESPACE 变量设置为“google”。

这是我在 Kubuntu 14.04 中遵循的步骤,应该类似于在 Mac OSX 中应该执行的步骤。这些会将 GFlags 源放在 /usr/local/src 中,并将库安装在 /usr/local/lib&include 目录中。最后一个命令(ldconfig)在系统中注册库。

cd /usr/local/src/
cp /path/to/downloaded/gflags-2.1.1.tar.gz .
sudo tar xzf gflags-2.1.1.tar.gz
cd /tmp
mkdir buildgflags
cd buildgflags
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_SHARED_LIBS=ON \
-DGFLAGS_NAMESPACE=google -G"Unix Makefiles" /usr/local/src/gflags-2.1.1/
make
sudo make install
sudo ldconfig

或者,您可以在 GLog 源中应用以下补丁(附在最后一个回复中):

https://code.google.com/p/google-glog/issues/detail?id=194

它基本上在 GLogs 单元测试源文件中包含后使用 gflags 的命名空间,如下所示:

#ifdef HAVE_LIB_GFLAGS
#include <gflags/gflags.h>
using namespace gflags;
#endif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多