0、系統環境
Ubuntu 16.04
GCC 5.4
1、安裝步驟
1)安裝 GFlags
運行如下指令:
1
2
3
4
5
6
|
git clone https://github.com/gflags/gflags.git
cd gflags
mkdir build && cd build
cmake .. -DGFLAGS_NAMESPACE=google -DCMAKE_CXX_FLAGS=-fPIC ..
make -j4
sudo make install
|
2)安裝 GLog:
安裝編譯工具:
1
|
sudo apt-get install autoconf automake libtool
|
運行如下指令安裝:
1
2
3
4
5
|
git clone https://github.com/google/glog
./autogen.sh
./configure
make -j8
sudo make install
|
常見問題
1、使用 GLog 報錯:libglog.a: error adding symbols: Bad value
在使用 GLog 的工程中遇到了如下錯誤:
1
2
3
|
/usr/bin/ld: /usr/local/lib/libglog.a(libglog_la-logging.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libglog.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
|
產生該問題的可能原因是在 64位系統中,不能鏈接 GLog 生成動態庫。修改方法是 GLog 需要使用如下方式編譯,加入 -fPIC 編譯選項:
1
|
./configure CPPFLAGS="-I/usr/local/include -fPIC" LDFLAGS="-L/usr/local/lib"
|
代替:
1
|
./configure
|
2、錯誤:undefined reference to `google::FlagRegisterer::FlagRegisterer
如果你在編譯 GLog 時遇到如下錯誤:
1
|
undefined reference to 'google::FlagRegisterer::FlagRegisterer'
|
可以嘗試先卸載 GFlags:
1
|
sudo apt-get purge libgflags-dev
|
然后重新編譯
另一種可能的解決方案是,在編譯 GLog 時請使用:
1
|
./configure CPPFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
|
代替:
1
|
./configure
|
3、錯誤:Make error: failed to link with libgflags.a
產生該問題的原因有可能是需要使用動態庫方式編譯 GFlags
將 GFlags 編譯時選項改成動態庫:
1
|
cmake .. -DBUILD_SHARED_LIBS=ON
|
並重新編譯 GFlags 和 GLog。
4、錯誤:something wrong with flag ‘flagfile’ in file ‘XXX.cc’
請參照 3 中的解決方案重新編譯 GFlags 和 GLog。