编译与安装
请参考下面的链接
https://blog.csdn.net/wei242425445/article/details/87968490
CMakeLists.txt的编写
cmake_minimum_required(VERSION 2.8)
project(demo)
find_package(gflags REQUIRED)
aux_source_directory(. DIRSRCS)
add_executable(demo ${DIRSRCS})
target_link_libraries(demo gflags)
注意:
需要链接gflags
Main.cpp的编写
#include <iostream>
#include <gflags/gflags.h>
DEFINE_bool(is_man, true, "Is that a man?");
DEFINE_string(name, "unknown", "Please input your name!");
int main(int argc, char *argv[])
{
gflags::ParseCommandLineFlags(&argc, &argv, true);
std::cout<<"note: "<< FLAGS_name<<std::endl;
google::ShutDownCommandLineFlags();
return 0;
}
运行
查看帮助文档
$ ./demo --help
demo: Warning: SetUsageMessage() never called
Flags from /home/wilson/code/cpp/test_gflags/Main.cpp:
-is_man (Is that a man?) type: bool default: true
-name (Please input your name!) type: string default: "unknown"
传参
$ ./demo --is_man=false
文件传参
配置文件config.txt
--is_man=false
--name="Wilson"
使用配置文件运行
$ ./demo -flagfile='config.txt
检查是否有内存泄漏
$ valgrind --leak-check=yes ./demo -flagfile='config.txt'
==6879== Memcheck, a memory error detector
==6879== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6879== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6879== Command: ./demo -flagfile=config.txt
==6879==
note: "Wilson"
==6879==
==6879== HEAP SUMMARY:
==6879== in use at exit: 0 bytes in 0 blocks
==6879== total heap usage: 107 allocs, 107 frees, 82,324 bytes allocated
==6879==
==6879== All heap blocks were freed -- no leaks are possible
==6879==
==6879== For counts of detected and suppressed errors, rerun with: -v
==6879== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)