参考:【多传感器融合定位】【ubuntu18.06配置环境】【ROS melodic】【g2o】【ceres】【Geographic】【gflags】【glog】【sophus】【GTSAM】【gtest】
# gflags
find_package(GFlags REQUIRED)
include_directories(${GFLAGS_INCLUDE_DIRS})
add_executable(xxx xxx.cpp)
target_link_libraries(xxx ${GFLAGS_LIBRARIES})
#include
参考:【slam十四讲第二版】【课本例题代码向】【第十三讲~实践:设计SLAM系统】的3.2.1 run_kitti_stereo.cpp
//
// Created by gaoxiang on 19-5-4.
//
#include
#include "myslam/visual_odometry.h"
DEFINE_string(config_file, "./config/default.yaml", "config file path");
int main(int argc, char **argv) {
// 解析gflags参数,只需要1行代码
google::ParseCommandLineFlags(&argc, &argv, true);
//gflags::ParseCommandLineFlags(&argc, &argv, true);
myslam::VisualOdometry::Ptr vo(
new myslam::VisualOdometry(FLAGS_config_file));
//assert(vo->Init() == true);//注释去这句话my_czy
vo->Init();
vo->Run();
return 0;
}
DEFINE_string(config_file, "./config/default.yaml", "config file path");,这个宏的三个参数分别是:config_file:参数名"./config/default.yaml":参数的默认值 "config file path":参数的说明FLAG_FLAGS_config_fileflagsgoogle::ParseCommandLineFlags(&argc, &argv, true);DECLARE_XXX的使用--help 显示所有文件的所有flag,按文件、名称排序,显示flag名、默认值和帮助
--helpshort 只显示执行文件中包含的flag,通常是 main() 所在文件
--version 打印执行文件的版本信息
摘自:gflags简明使用指南
//test2.cc
#include
#include
#include //gflags库的头文件
//else header file
#define LOG
DEFINE_string(ip, "127.0.0.1", "IP address");
DEFINE_int32(port, 8080, "port");
class Server{
public:
Server(const std::string& ip, uint16_t port) : _ip(ip), _port(port) {
std::cout << "Init Server..." << std::endl;
#ifdef LOG
std::cout << "ip : " << _ip << std::endl;
std::cout << "port: " << _port << std::endl;
#endif
std::cout << "Init OK!" << std::endl;
}
//else code
private:
std::string _ip;
uint16_t _port;
//else code
};
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
Server* pserver = new Server(FLAGS_ip, FLAGS_port);
return 0;
}
g++ test2.cc -o test2 -lgflags -lpthread
#include
#include "gflags/gflags.h"
// 定义gflags
DEFINE_bool(foo, false, "a simple gflags named foo, default value is flase, wuruilong, 2018-08-16");
DEFINE_int32(thread_num, 10, "thread number, default value is 10, wuruilong, 2018-08-16");
int main(int argc, char **argv) {
// 解析gflags参数,只需要1行代码
google::ParseCommandLineFlags(&argc, &argv, true);
// 使用gflags
if (FLAGS_foo) {
std::cout << "foo is true" << std::endl;
} else {
std::cout << "foo is false" << std::endl;
}
// 使用gflags
int thread_num = FLAGS_thread_num;
std::cout << "thread number:" << thread_num << std::endl;
return 0;
}
g++ simple_gflags.cpp -I./gflags-2.0/src -L./ -lgflags
./a.out
输出:
foo is false
thread number:10
./a.out -foo=true
输出:
foo is true
thread number:10
./a.out -foo=true -thread_num=99
输出:
foo is true
thread number:99
gflags_def.cpp#include "gflags/gflags.h"
// 定义gflags
DEFINE_bool(add_new_feature_x, false, "x feature, gaojingying, 2018-08-16");
DEFINE_bool(add_new_featrue_y, false, "y feature, xiechao, 2018-08-16");
DEFINE_bool(fix_memory_leak_bug, false, "fix memory leak bug, xiechao, 2018-08-16");
DEFINE_bool(fix_cpu_high_bug, false, "fix cpu high bug, xiechao, 2018-08-16");
DEFINE_int32(thread_pool_worker_num, 10, "thread pool worker number, default value is 10, ligang, 2018-08-16");
DEFINE_string(server_ip, "127.0.0.1", "x server's ip address, gaojingying, 2018-08-16");
DECLARE_声明#include
#include
#include
#include "gflags/gflags.h"
// 声明gflags
DECLARE_bool(add_new_feature_x);
DECLARE_bool(add_new_featrue_y);
DECLARE_bool(fix_memory_leak_bug);
DECLARE_bool(fix_cpu_high_bug);
DECLARE_int32(thread_pool_worker_num);
DECLARE_string(server_ip);
void Work(std::string &name) {
name = "feature";
// 启用x功能
if (FLAGS_add_new_feature_x) {
name += "_x";
}
// 启用y功能
if (FLAGS_add_new_featrue_y) {
name += "_y";
}
char *value = new char[100];
snprintf(value, 100, "thread number: %d", FLAGS_thread_pool_worker_num);
name = name + "," + value + "," + FLAGS_server_ip;
// 留下消缺记录
if (FLAGS_fix_memory_leak_bug) {
delete []value;
}
}
int main(int argc, char **argv) {
google::ParseCommandLineFlags(&argc, &argv, true);
std::string name;
Work(name);
std::cout << name << std::endl;
return 0;
}
g++ main.cpp gflags_def.cpp –I./gflags-2.0/src -L./ -lgflags
.gflags文件demo_project.gflags-add_new_feature_x=false
-add_new_featrue_y=true
-fix_memory_leak_bug=true
-fix_cpu_high_bug=false
-thread_pool_worker_num=20
-server_ip="127.0.0.1"
./a.out
输出:
feature,thread number: 10,127.0.0.1
./a.out --flagfile=demo_project.gflags
输出:
feature_x,thread number: 20,"127.0.0.1"
.gfalgs演示./a.out --flagfile=demo_project.gflags
输出:
feature_y,thread number: 20,"127.0.0.1"