Install gflags
gflags, commandline flags module for C++.
Environment
- Ubuntu 12.04 64bit
Install gflags.
Download gflags-2.0.tar.gz
$ tar zxvf gflags-2.0.tar.gz $ cd gflags-2.0 $ ./configure $ make $ sudo make install $ sudo ldconfig $ cd .. $ rm -rf gflags-2.0
Code
// gflags_test.cc #include <iostream> #include <gflags/gflags.h> DEFINE_bool(my_bool, false, "bool type"); DEFINE_int32(my_int32, 123, "int32 type"); DEFINE_int64(my_int64, 1245L, "int64 type"); DEFINE_uint64(my_uint64, 12345L, "uint64 type"); DEFINE_double(my_double, 1.234L, "double type"); DEFINE_string(my_string, "init", "string type"); int main(int argc, char** argv) { google::ParseCommandLineFlags(&argc, &argv, true); std::cout << "my_bool: " << FLAGS_my_bool << std::endl; std::cout << "my_int32: " << FLAGS_my_int32 << std::endl; std::cout << "my_int64: " << FLAGS_my_int64 << std::endl; std::cout << "my_uint64: " << FLAGS_my_uint64 << std::endl; std::cout << "my_double: " << FLAGS_my_double << std::endl; std::cout << "my_string: " << FLAGS_my_string << std::endl; return 0; }
Build
$ g++ gflags_test.cc -lgflags
Test
$ ./a.out my_bool: 0 my_int32: 123 my_int64: 1245 my_uint64: 12345 my_double: 1.234 my_string: init $ ./a.out --my_bool=true --my_int32=234 --my_int64=2345 --my_uint64=23456 \ > --my_double=2.345 --my_string=new my_bool: 1 my_int32: 234 my_int64: 2345 my_uint64: 23456 my_double: 2.345 my_string: new