安装

install gflags just run:

sudo ./install_gflags.sh

构建文件install_gflags.sh,内容如下:

#!/bin/bash -x

cd gflags
if [ $? -ne 0 ]
then
    echo "gflags directory not found"	
    git clone https://github.com/gflags/gflags.git
    if [ $? -ne 0 ]
    then
        echo "Unable to perform git clone https://github.com/gflags/gflags.git"
        exit
    fi
fi
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
make install

install glog just do:

sudo ./install_glog.sh

install_glog.sh内容如下:

#!/bin/bash -x

cd glog
if [ $? -ne 0 ]
then
    echo "glog directory not found"	
    git clone -b v0.4.0 https://github.com/google/glog
    if [ $? -ne 0 ]
    then
        echo "Unable to perform git clone https://github.com/google/glog"
        exit
    fi
fi
sudo apt-get install autoconf automake libtool
cd glog
./autogen.sh
./configure
make -j 6
sudo make install

使用

gflag

https://www.cnblogs.com/tianyajuanke/p/3467572.html
gflag 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;
  }

  // Get all the flags and output them in all_flags.
  std::vector<google::CommandLineFlagInfo> all_flags;
  GetAllFlags(&all_flags);
  for (const gflags::CommandLineFlagInfo& flag_info : all_flags) {
      std::cout << "FLAGS_" << flag_info.name << "=" << flag_info.current_value << std::endl;
  }

  google::ShutDownCommandLineFlags();
  return 0;
}

运行:

g++ test_gflag.cc -lgflags
./a.out -ip="211.152.52.106" -port=8080 -isvip=true

输出:

ip:211.152.52.106
port:8080
isvip:1

此外,可以指定一个配置好的参数文件(--flagfile)。对于gflags中的--flagfile用法,不管二进制节点后面接几个–flagfile=的配置文件,他们之间的关系是衔接的。但是对于几个文件中都有的flag, 哪个–flagfile=文件在后面,哪个变量就是最终代码使用的值。
如:

./app_node --flagfile=app_1.conf --flagfile=app_2.conf

app_1.conf中有一句--system_version=1.0.0.0
app_2.conf文件中有一句--system_version=2.0.0.0
那么由于--flagfile=app_2.conf写在后面,所以代码最终FLAGS_system_version使用的值是2.0.0.0

glog

推荐博客:
glog基本使用
GLog
Google-glog 日志库使用手记
使用GLOG控制C++程序输出

glog demo

#include <iostream>
#include <gflags/gflags.h>
#include <gflags/gflags_declare.h>
#include <glog/logging.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");
// DECLARE_double(test);
DEFINE_double(test, 0.01, "ddd");

int main(int argc, char** argv)
{
  // gflag使用:
  google::ParseCommandLineFlags(&argc, &argv, true);
  std::cout<<"test:"<<FLAGS_test<<std::endl;
  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();

  // glog使用:
  google::InitGoogleLogging(argv[0]); // 初始化GLog库
  FLAGS_logtostderr = true;  //设置日志消息是否转到标准输出而不是日志文件, 即true:不写入文件,只在终端显示
  FLAGS_log_dir = "./log";   // 需要在该文件同路径下建一个文件夹:log
  google::SetLogDestination(google::GLOG_INFO, "./log/log_");
  google::SetStderrLogging(google::GLOG_INFO);
  google::SetLogFilenameExtension("log_");
  FLAGS_v = 4; //设置最大显示等级(超过该等级将不记录log)
  FLAGS_colorlogtostderr = true;  // Set log color
  LOG(INFO) << "There is INFO !!!";
  LOG(WARNING) << "There is WARNING !!!";
  LOG(ERROR) << "There is error !!!";
  //VLOG用来自定义日志, 可以在括号内指定log级别
  VLOG(1) << "[Custom log(VLOG)] Level 1!";
  VLOG(2) << "[Custom log(VLOG)] Level 2!";
  VLOG(3) << "[Custom log(VLOG)] Level 3!";
  VLOG(4) << "[Custom log(VLOG)] Level 4! This is used for detailed message which need not to be printed each time.";
  VLOG(5) << "[Custom log(VLOG)] Level 5! On this level messages are print as detail as possible for debugging.";
  LOG(FATAL) << "I am FATAL!";
  google::ShutDownCommandLineFlags();
  
  return 0;
}

运行命令:

g++ test_gflag.cc -lgflags -lglog
./a.out

输出结果:

test:0.01
ip:127.0.0.1
port:80
I0414 09:19:35.435585  4632 vector.cc:34] There is INFO !!!
W0414 09:19:35.435724  4632 vector.cc:35] There is WARNING !!!
E0414 09:19:35.435798  4632 vector.cc:36] There is error !!!

一些错误

报错:

error while loading shared libraries: libglog.so.0: cannot open shared object file: No such file or directory
error while loading shared libraries xxx.so,cannot open shared object file

可能的原因:安装到了/usr/local/lib,而运行是未指定该路径,可以尝试执行下面代码,临时添加路径,然后重新编译代码。

export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/usr/local/lib

如果希望该环境永久有效,则需要将其添加到profile中:

# 打开/etc/profile文件
sudo vim /etc/profile
# 在文件末尾加上下面这行
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/usr/local/lib

参考:

更多推荐