1.安装gulags:
step1:
git上拉取源代码:
git clone https://github.com/gflags/gflags
step2:
编译和安装gflags,依次执行:
cd gflags
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=ON -DINSTALL_HEADERS=ON -DINSTALL_SHARED_LIBS=ON -DINSTALL_STATIC_LIBS=ON ..
make -j4
make install
step3:
确定/etc/ld.so.conf文件下有/usr/local/lib和/usr/lib文件夹:
/usr/local/lib
/usr/lib
加载:
ldconfig
2.使用gflags:
编辑test.cc
#include<gflags/gflags.h>
#include<iostream>
using namespace std;
DEFINE_string(test_flag, "test flag.", "is a test");
DEFINE_bool(x,true,"bool flag");
int main(int argc, char **argv)
{
google::ParseCommandLineFlags(&argc, &argv, true);
if(FLAGS_x)
cout<<FLAGS_test_flag<<endl;
return 0;
}
g++ test.cc -o test -lgflags -lpthread
执行
./test --test_flag="123"
输出结果:
123