gflags的使用

编译与安装

请参考下面的链接
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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值