How to use Gflags?

Share:
How to use Gflags? # Resources - [GFlags使用文档](http://www.yeolar.com/note/2014/12/14/gflags/) - How To Use gflags (formerly Google Commandline Flags), https://gflags.github.io/gflags/ # Install Install from source code: ```sh git clone https://github.com/gflags/gflags.git cd gflags cmake .. make -j2 sudo make install ``` Or install use apt-get ```sh sudo apt-get install libgflags* ```` # CMake Find gflags installation. The gflags_DIR variable must be set to the /lib/cmake/gflags directory containing the gflags-config.cmake file if is a non-standard location. Otherwise, CMake should find the gflags installation automatically. ```sh find_package(gflags REQUIRED) add_executable(foo main.cc) target_link_libraries(foo gflags::gflags) ``` # DEFINE: Defining Flags In Program Defining a flag is easy: just use the appropriate macro for the type you want the flag to be, as defined at the bottom of gflags/gflags.h. Here’s an example file, foo.cc: Include DEFINE_bool(big_menu, true, “Include ‘advanced’ options in the menu listing”); DEFINE_string(languages, “english,french,german”, “comma-separated list of languages to offer in the ‘lang’ menu”); DEFINE_bool defines a boolean flag. Here are the types supported: ```cpp DEFINE_bool: boolean DEFINE_int32: 32-bit integer DEFINE_int64: 64-bit integer DEFINE_uint64: unsigned 64-bit integer DEFINE_double: double DEFINE_string: C++ string ``` # Accessing the Flag All defined flags are available to the program as just a normal variable, with the prefix FLAGS_ prepended. In the above example, the macros define two variables, FLAGS_big_menu (a bool), and FLAGS_languages (a C++ string). You can read and write to the flag just like any other variable: ```cpp if (FLAGS_consider_made_up_languages) FLAGS_languages += “,klingon”; // implied by —consider_made_up_languages if (FLAGS_languages.find(“finnish”) != string::npos) HandleFinnish(); ``` You can also get and set flag values via special functions in gflags.h. That’s a rarer use case, though. Miscellaneous Notes If your application has code like this: ```cpp # define STRIP_FLAG_HELP 1 // this must go before the #include ```

No comments