Configure google glog and gflags for c++

user3235200 picture user3235200 · May 10, 2014 · Viewed 14.1k times · Source

I've been trying to configure the google logging library glog for my C++ application but I can't find any information about how to actually get it to work, and the error messages are less than helpful.

This is the example code I'm trying to execute, and I'm executing ./myapp --v=2, but I get "ERROR: unknown command line flag 'v'". Is there any documentation for this library, or do anyone know how to correctly configure it?

#include <glog/logging.h>
#include <gflags/gflags.h>

int main(int argc, char** argv) {
    google::InitGoogleLogging(argv[0]);
    google::ParseCommandLineFlags(&argc, &argv, true);

    VLOG(1) << "I'm printed when you run the program with --v=1 or higher";
    VLOG(2) << "I'm printed when you run the program with --v=2 or higher";
    return 0;
}

Answer

Elias Kouskoumvekakis picture Elias Kouskoumvekakis · Sep 13, 2014

GLog needs GFlags compiled in the "google" namespace instead of the now default "gflags" namespace.

In order to set this namespace you must compile and install gflags from source and set the GFLAGS_NAMESPACE variable to "google".

Here are the steps I followed in Kubuntu 14.04 and should be similar to what you should do in Mac OSX. These will place the GFlags source in /usr/local/src and install the library in the /usr/local/lib&include directories. The last command (ldconfig) registers the library in the system.

cd /usr/local/src/
cp /path/to/downloaded/gflags-2.1.1.tar.gz .
sudo tar xzf gflags-2.1.1.tar.gz
cd /tmp
mkdir buildgflags
cd buildgflags
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_SHARED_LIBS=ON \
-DGFLAGS_NAMESPACE=google -G"Unix Makefiles" /usr/local/src/gflags-2.1.1/
make
sudo make install
sudo ldconfig

Alternatively you can apply the following patch in the GLog source (attached in the last reply):

https://code.google.com/p/google-glog/issues/detail?id=194

It basically uses the namespace of gflags after the includes on the GLogs unit test source files like so:

#ifdef HAVE_LIB_GFLAGS
#include <gflags/gflags.h>
using namespace gflags;
#endif