2017-11-14 1 views
0

は私が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のバージョンのそれをですか?

答えて

0

あなたは、コンパイルコマンドラインでアーカイブが含まれていませgflagsをリンクする必要があります。

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

のみ静的ライブラリがある場合は、G ++リンカがそれを処理することができます。だから、基本的にコンパイラ/リンカーにあなたに必要なことを伝える必要があります。gflags

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