The gflags package contains a C++ library that implements commandline flags processing. It includes built-in support for standard types such as string and the ability to define flags in the source file in which they are used.
https://zhuanlan.zhihu.com/p/108477489
gflags是什么?
gflags是google开源的命令行标记处理库;
那么什么是命令行标记呢?顾名思义就是当运行一个可执行文件时,由用户为其指定的标记,形如:fgrep -l -f ./test ccc jjj
注意上述命令,-l与-f ./test是命令行标记,而ccc与jjj是命令行参数,因为这两者不是以破折号开头的。一般的一个可执行文件,允许用户为其传入命令行标记以及参数,如上述例子,-l是一个不带参数的标记,-f是一个带了参数“./test”的标记,而gflags可以解析这些标记以及参数并将其存储在某些数据结构中。
https://blog.csdn.net/qq_22634949/article/details/101718879
git clone https://github.com/gflags/gflags.git
cd gflags
mkdir build && cd build
cmake .. -DGFLAGS_NAMESPACE=google -DCMAKE_CXX_FLAGS=-fPIC ..
make -j4
sudo make install
注意,如在后期make时遇到错误something wrong with flag 'flagfile' in file 'XXX.cc',eg,something wrong with flag 'logtostderr' in file '/root/glog/src/logging.cc', you are possibly trying to link file XXX.cc both statically and dynamically into the executable。因此,除静态链接库以外,还需要build一个动态链接库。使用cmake时,将BUILD_SHARED_LIBS设置为ON,并重新编译gflags。如下
git clone https://github.com/gflags/gflags.git
cd gflags
mkdir build && cd build
cmake -DGFLAGS_NAMESPACE=google -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_SHARED_LIBS=ON ..
make -j4
sudo make install
sudo ldconfig
最后一句sudo ldconfig,加载动态链接库,一般是编译安装软件时候出现动态链接库报错需要手动加载。
https://blog.csdn.net/qq_39451578/article/details/115679961?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242
main.cpp
#include
#include
#include
#include
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:"<
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(mout)
# set(CMAKE_BUILD_TYPE "Debug") # Debug/Release
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
find_package(gflags REQUIRED)
include_directories(${GFLAGS_INCLUDE_DIR})
find_package(glog REQUIRED)
include_directories(${GLOG_INCLUDE_DIRS})
add_executable(mout main.cpp)
target_link_libraries(mout gflags glog)
目录如下
https://www.nowcoder.com/discuss/652985?channel=-1&source_id=discuss_terminal_discuss_sim_nctrack&ncTraceId=cbbaed3185b94ea98d63e92d614e0334.1640.16214572630889822
作者:fibonaccii
链接:https://www.nowcoder.com/discuss/652985?channel=-1&source_id=discuss_terminal_discuss_sim_nctrack&ncTraceId=cbbaed3185b94ea98d63e92d614e0334.1640.16214572630889822
来源:牛客网
$ tree -L 2
.
└── demo
├── CMakeLists.txt
├── build # 目前是空的,用于后面编译输出
├── gflags # 上面clone下来的代码
└── main.cc # 目前是空的
cmake_minimum_required(VERSION 2.8.12)
project(gflags_demo)
add_subdirectory(gflags) # 添加子目录
add_executable(main main.cc) # 生成目标文件 main
target_link_libraries(main gflags::gflags) # 链接到 gflags库
作者:fibonaccii
链接:https://www.nowcoder.com/discuss/652985?channel=-1&source_id=discuss_terminal_discuss_sim_nctrack&ncTraceId=cbbaed3185b94ea98d63e92d614e0334.1640.16214572630889822
来源:牛客网
#include
#include
DEFINE_bool(big_menu, true, "Include 'advance' option in the menu listing");
DEFINE_string(languages, "english, french, chinese", "comma-separated list of languages to offer in the 'lang' menu");
DEFINE_uint32(age, 10, "age");
int main(int argc, char** argv) {
std::cout<
作者:fibonaccii
链接:https://www.nowcoder.com/discuss/652985?channel=-1&source_id=discuss_terminal_discuss_sim_nctrack&ncTraceId=cbbaed3185b94ea98d63e92d614e0334.1640.16214572630889822
来源:牛客网
$ cd build && cmake .. && make # 进入 demo/build 目录,再编译
$./main
big_menu: true
languages: english, french, chinese
age: 10
$ make
$ ./main -age 100 -languages "123456"
big_menu: true
languages: 123456
age: 100
对于命令行输入格式,gflags
提供了四种格式,以age
变量为例:
./main -age 100
./main -ag=100
./main --age 100
./main --age=100
使用--helpshort
选项,就会显示这些变量的相关信息:
$ ./main --helpshort
main: Warning: SetUsageMessage() never called
Flags from /Users/self_study/Cpp/OpenSource/demo/main.cc:
-age (age) type: uint32 default: 10
-big_menu (Include 'advance' option in the menu listing) type: bool
default: true
-languages (comma-separated list of languages to offer in the 'lang' menu)
type: string default: "english, french, chinese"
如果某个项目中使用了 gflags 定义了很多全局变量,分布在不同的文件中,你可以使用--helpfull选项来查看这些变量:
作者:fibonaccii
链接:https://www.nowcoder.com/discuss/652985?channel=-1&source_id=discuss_terminal_discuss_sim_nctrack&ncTraceId=cbbaed3185b94ea98d63e92d614e0334.1640.16214572630889822
来源:牛客网
% ./main --helpfull
main: Warning: SetUsageMessage() never called
Flags from /Users/self_study/Cpp/OpenSource/demo/gflags/src/gflags.cc:
-flagfile (load flags from file) type: string default: ""
-fromenv (set flags from the environment [use 'export FLAGS_flag1=value'])
type: string default: ""
-tryfromenv (set flags from the environment if present) type: string
default: ""
-undefok (comma-separated list of flag names that it is okay to specify on
the command line even if the program does not define a flag with that
name. IMPORTANT: flags in this list that have arguments MUST use the
flag=value format) type: string default: ""
Flags from /Users/self_study/Cpp/OpenSource/demo/gflags/src/gflags_completions.cc:
-tab_completion_columns (Number of columns to use in output for tab
completion) type: int32 default: 80
-tab_completion_word (If non-empty, HandleCommandLineCompletions() will
hijack the process and attempt to do bash-style command line flag
completion on this value.) type: string default: ""
Flags from /Users/self_study/Cpp/OpenSource/demo/gflags/src/gflags_reporting.cc:
-help (show help on all flags [tip: all flags can have two dashes])
type: bool default: false
-helpfull (show help on all flags -- same as -help) type: bool
default: false currently: true
-helpmatch (show help on modules whose name contains the specified substr)
type: string default: ""
-helpon (show help on the modules named by this flag value) type: string
default: ""
-helppackage (show help on all modules in the main package) type: bool
default: false
-helpshort (show help on only the main module for this program) type: bool
default: false
-helpxml (produce an xml version of help) type: bool default: false
-version (show version and build info and exit) type: bool default: false
Flags from /Users/self_study/Cpp/OpenSource/demo/main.cc:
-age (age) type: uint32 default: 10
-big_menu (Include 'advance' option in the menu listing) type: bool
default: true
-languages (comma-separated list of languages to offer in the 'lang' menu)
type: string default: "english, french, chinese"