欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

glog和gflags的使用

程序员文章站 2022-03-20 13:58:21
...

glog:日志

gflags:命令行参数解析

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

DEFINE_string(input_dir, "", "Path to the input data directory."); //不一定放在main函数中
DEFINE_string(output_dir, "", "Path to the output result directory.");
DEFINE_bool(show_image, false, "Whether to show images"); // 调试开关
DEFINE_double(rate, 1.0,""); // 代替宏定义

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

    string input_dir = FLAGS_input_dir;
    string input_dir = FLAGS_output_dir;

    LOG(INFO|WARNING|FATAL) << "xxx" << "xxx";
    LOG_IF(FATAL, 条件)
}

run:./main --input_dir="xxx" --output_dir="xxx"

CMakeList.txt:

find_package(gflags REQUIRED)
include_directories(${gflags_INCLUDE_DIR})
find_package(Glog REQUIRED)
include_directories(${GLOG_INCLUDE_DIRS})

add_executable(main main.cpp)
target_link_libraries(main glog::glog gflags)