Introduction to C + + using gflags library functions

Posted by avillanu on Sun, 02 Jan 2022 14:28:51 +0100

Introduction:

gflags is google's open source project for handling command line parameters. Using c + + development, with python interface.

The definition of flag can be scattered in various source codes instead of being put together. A source file can define some of its own flags, which can be used by applications linked to the file. This makes it easy to reuse code. If different files define the same flag, an error will be reported when linking.

1. Download and install:

Project home page: http://gflags.github.io/gflags/

Download address: https://github.com/gflags/gflags

Specific steps:

1.1 download

git clone https://github.com/gflags/gflags

1.2 compilation and installation

cd gflags			
cmake . 	        #Note: CMAKE version needs to be greater than 2.8 More than 4		
make -j 24 			
sudo make install	

1.3 supplement CMAKE upgrade and installation

# Check cmake version
cmake --version

#Installation and compilation dependencies
yum install -y gcc gcc-c++ build-essential autoconf libtool pkg-config

#Quick install and update version
wget -q -O cmake-linux.sh https://github.com/Kitware/CMake/releases/download/v3.17.0/cmake-3.17.0-Linux-x86_64.sh
sh cmake-linux.sh -- --skip-license --prefix=/usr
rm cmake-linux.sh

2. Simple usage:

2.1 header file to include

#include <gflags/gflags.h>

2.2 define the required command line parameter variables

DEFINE_bool(Variable name, Default value, "describe");
#Example:
DEFINE_bool(daemon, false, "Is Daemon");
DEFINE_string(ip, "127.0.0.1", "server ip");
DEFINE_int32(port,8088, "listen port");

The types of command line parameters include:

[gflags Define type]    [description]
  DEFINE_bool	    Boolean type
  DEFINE_int32	    32 Bit integer
  EFINE_int64	    64 Bit integer
  DEFINE_uint64	  Unsigned 64 bit integer
  EFINE_double	  Floating point type double
  DEFINE_string	  C++ string type

2.3 when calling the main function, you need to add:

google::ParseCommandLineFlags(&argc, &argv, true);

Among them, argc and argv are the entry parameters of main. Because this function will change their values, they are all passed in as pointers. The third parameter is called remove_flags. If it is true, ParseCommandLineFlags will remove the IDS and their parameters from argv and reduce the value of argc accordingly. Argv then retains only command line parameters.
Instead, remove_ If flags is false, ParseCommandLineFlags will keep argc unchanged, but their order will be readjusted so that the ID is in front.

2.4 at the end of use, it is necessary to add:

 google::ShutDownCommandLineFlags();

2.5 complete examples

#include <iostream>
#include <gflags/gflags.h>

DEFINE_string(name, "Tom", "name");
DEFINE_int32(age, 18, "age");

int main(int argc, char** argv) {
	google::ParseCommandLineFlags(&argc, &argv, true);
	std::cout << "your name is : " << FLAGS_name
        << ", your age is: " << FLAGS_name << std::endl;
    google::ShutDownCommandLineFlags();
    return 0;
}

Compilation and output:

g++ gflags_test.cc -o gflags_test -lgflags -lpthread 		#-l link library for compilation
./gflags_test                                           	# use
your name is : Tom, your age is: 18 				        # output

3. Advanced use:

gulags are most commonly used for cross file use.

Implement the gflags variable defined in one file to access another file. Using DECLARE_, Its function is equivalent to declaring variables with {extern}.

Usually in cpp file, and then in the corresponding The DECLARE variable in the h file or unit test. If you want to use the variable, you can include the header file of the variable.

Topics: C++