c++ - gflags 链接器错误 : undefined reference

标签 c++ compiler-errors linker-errors gflags

我安装了 gflags:

$ 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版本问题?

最佳答案

您需要针对 gflags 进行链接,而不是在编译命令行中包含存档:

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

如果只有一个静态库,g++ 链接器可以处理。所以基本上你只需要告诉编译器/链接器你需要 gflags:

$ g++ a.cpp -o test -lgflags

关于c++ - gflags 链接器错误 : undefined reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47279920/

相关文章:

c++ - 较大年份 (2048) 的纪元 unix 时间值不正确

c - 为什么即使我将.c文件作为参数提供,gcc仍无法识别我的.c文件?

objective-c - CMSampleBufferGetImageBuffer CVImageBufferGetEncodedSize 制作问题

c++ - 如果尝试将负数分配给未签名的容器,是否有导致编译错误的方法?

c++ - C++中的非类类型错误

c++ - 使用 MinGW 编译器时出现链接错误(找不到 __main)

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - 在不同文件中编写类时聚合不起作用

c++ - CPP 字符串的字符串分词器?

c++ - 模板类中的友元运算符<<