一、安裝配置
下載地址:
https://code.google.com/p/gflags/downloads/list
解壓安裝:
tar zxvf gflags-2.0.tar.gz && cd gflags-2.0 && ./configure && make
頭文件目錄為 /src/gflags ,鏈接庫為 .libs/libgflags.{a,so}
簡單 Demo 如下:
#include <iostream> #include <gflags/gflags.h> DEFINE_bool(isvip, false, "If Is VIP"); DEFINE_string(ip, "127.0.0.1", "connect ip"); DECLARE_int32(port); DEFINE_int32(port, 80, "listen port"); int main(int argc, char** argv) { google::ParseCommandLineFlags(&argc, &argv, true); std::cout<<"ip:"<<FLAGS_ip<<std::endl; std::cout<<"port:"<<FLAGS_port<<std::endl; if (FLAGS_isvip) { std::cout<<"isvip:"<<FLAGS_isvip<<std::endl; } google::ShutDownCommandLineFlags(); return 0; }
鏈接時使用 -gflags ,運行使用 ./gflags -ip="211.152.52.106" -port=8080 -isvip=true ,但是很遺憾,使用 valgrind 檢測有內存泄漏。
輸出結果如下:
ip:211.152.52.106 port:8080 isvip:1
官方文檔:
http://google-gflags.googlecode.com/svn/trunk/doc/gflags.html