1

I installed gflags:

$ ls /usr/local/lib/ | grep gflags
libgflags.a
libgflags_nothreads.a
$ ls /usr/local/include/ | grep gflags
gflags

And included <gflags/gflags.h>

#include <gflags/gflags.h>

DEFINE_bool(a, false, "test");

int main(int argc, char **argv) {
    gflags::ParseCommandLineFlags(&argc, &argv, true);
    return 0;
}

But i got a linker-error!

$ g++ -lgflags a.cpp 
/tmp/cchKYAWZ.o: In function `main':
/home/wonter/gflags-2.2.1/build/a.cpp:6: undefined reference to `google::ParseCommandLineFlags(int*, char***, bool)'
/tmp/cchKYAWZ.o: In function `__static_initialization_and_destruction_0(int, int)':
/home/wonter/gflags-2.2.1/build/a.cpp:3: undefined reference to `google::FlagRegisterer::FlagRegisterer<bool>(char const*, char const*, char const*, bool*, bool*)'
collect2: error: ld returned 1 exit status

I tried $ g++ /usr/local/lib/libgflags.a a.cpp -o test, but got a same error.

My platform is Ubuntu 17.10, GCC version is gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3)

Is it because of my GCC version of the problem?

Wonter
  • 293
  • 1
  • 5
  • 15

1 Answers1

2

You need to link against gflags not include the archive in compile command line:

$ g++ -Wl,-Bstatic -lgflags,--as-needed a.cpp -o test

If there is only a static library, g++ linker can handle that. So basically you just need to tell compiler/linker that you need gflags:

$ g++ a.cpp -o test -lgflags
sorush-r
  • 10,490
  • 17
  • 89
  • 173