Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure google glog and gflags for c++

Tags:

c++

logging

glog

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;
}
like image 279
user3235200 Avatar asked May 10 '14 13:05

user3235200


1 Answers

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
like image 67
Elias Kouskoumvekakis Avatar answered Sep 27 '22 21:09

Elias Kouskoumvekakis