gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:55:13 25 4
gpt4 key购买 nike

我安装了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com