最近项目里面要用 gflags 库,然后通过brew install gflags
安装 gflags ,但是一通折腾就是编译不通过,不知道大伙有没有见过这样的问题,下面是编译不通过的代码
#include <gflags/gflags.h>
#include <iostream>
DECLARE_string(test);
int main(int argc, char* argv[]) {
std::cout << FLAGS_test << std::endl;
return 0;
}
Undefined symbols for architecture x86_64:
"fLS::FLAGS_test", referenced from:
_main in test-d79d53.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
下面是我尝试过的几个不同的编译命令,均无效:
g++ test.cpp -lgflags
g++ test.cpp /usr/local/lib/libgflags.dylib
g++ -L /usr/local/Cellar/gflags/2.2.2/lib -lgflags test.cpp
目前分析了一下,通过通过brew install gflags
安装的 gflags ,只提供了 dylib 动态链接库,如下所示:
[/usr/local/Cellar/gflags/2.2.2/lib]$ ls
cmake libgflags.2.2.dylib libgflags_nothreads.2.2.2.dylib libgflags_nothreads.dylib
libgflags.2.2.2.dylib libgflags.dylib libgflags_nothreads.2.2.dylib pkgconfig
[/usr/local/lib]$ ls | grep gflags
libgflags.2.2.2.dylib
libgflags.2.2.dylib
libgflags.dylib
libgflags_nothreads.2.2.2.dylib
libgflags_nothreads.2.2.dylib
libgflags_nothreads.dylib
是否使用动态链接库需要指定某些额外的选项,大家有类似的经验吗?
1
ingnoscemihi 2023-07-03 11:59:07 +08:00
brew link gflags
clang++ test.cpp -o test ok 啦 |
2
rainboat OP @ingnoscemihi 执行 brew link gflags 提示我“Warning: Already linked”,再次编译还是同样的问题,难顶
|
3
tangtang369 2023-07-03 15:29:12 +08:00
```
#include <iostream> #include <gflags/gflags.h> //DECLARE_string(test); DEFINE_string(languages, "english,french,german","comma-separated list of languages to offer in the 'lang' menu"); int main(int argc, char* argv[]) { std::cout << FLAGS_languages << std::endl; return 0; } ``` 用 DEFINE_string 可以编译通过 |
4
rainboat OP @tangtang369 还真是可以编译了,大佬牛
|
5
rainboat OP @tangtang369 不过好像运行 ./test --languages="chinese,japanese,korean",输出的结果还是 english,french,german ,这个命令行设置不起作用
|
6
tangtang369 2023-07-04 10:03:58 +08:00 1
@rainboat 要解析输入
``` #include <iostream> #include <gflags/gflags.h> //DECLARE_string(test); DEFINE_string(languages, "english,french,german","comma-separated list of languages to offer in the 'lang' menu"); int main(int argc, char* argv[]) { gflags::ParseCommandLineFlags(&argc, &argv, true); std::cout << FLAGS_languages << std::endl; return 0; } ``` |
7
rainboat OP @tangtang369 成功了,太感谢了,你真是我的哥!
|