我安装了gflag:
$ ls /usr/local/lib/ | grep gflags
libgflags.a
libgflags_nothreads.a
$ ls /usr/local/include/ | grep gflags
gflags和包含的<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;
}但是我得到了一个链接器错误!
$ 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我尝试了$ g++ /usr/local/lib/libgflags.a a.cpp -o test,但得到了相同的错误。
我的平台是Ubuntu 17.10,GCC的版本是gcc version 7.2.0 (Ubuntu 7.2.0-8ubuntu3)
是不是因为我的GCC版的问题?
发布于 2017-11-14 15:34:56
您需要链接到gflags,而不是在编译命令行中包含归档文件:
$ g++ -Wl,-Bstatic -lgflags,--as-needed a.cpp -o test如果只有一个静态库,g++链接器可以处理。所以基本上你只需要告诉编译器/链接器你需要gflags
$ g++ a.cpp -o test -lgflagshttps://stackoverflow.com/questions/47279920
复制相似问题