使用google protobuf時,出現錯誤
/usr/bin/ld: /usr/local/lib/libprotobuf.a(message_lite.o): relocation R_X86_64_32S against `_ZTVN6google8protobuf11MessageLiteE' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libprotobuf.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
搜了下,幾篇文章如下:
查看整個編譯過程,編譯過程中看到沒有-fPIC選項,如何加進去
看protobuffer的README和INSTALL文檔,看到需要添加特殊編譯選項,需要在執行configure的時候引入,
於是make clean; make uninstall
./configure CXXFLAGS=-fPIC
查看編譯文件,發現已經有了編譯選項-fPIC,再次執行編譯安裝
重新編譯程序,這次沒有報錯,沒有出錯,問題解決。
另外回答;
原因:
-fPIC 是個神馬東東呢?
選項 -fPIC
PIC是Position-Independent-Code的縮寫。在計算機系統中,PIC和PIE(Position-IndependentExecutable)是可以在主存中不同位置執行的目標代碼。PIC經常被用在共享庫中,這樣就能將相同的庫代碼為每個程序映射到一個位置,不用擔心覆蓋掉其他程序或共享庫。
因為so動態庫編譯的時候加上了 -fPIC,但是連接的 libprotobuf.a文件並不是 -fPIC生成的,所以就報錯了。
那就是說連接的 libprotobuf.aa 文件,也需要加上 -fPIC 選項進行編譯了。
關於-fPIC:http://blog.sina.com.cn/s/blog_54f82cc201011op1.html
/usr/bin/ld: libs/X86_64/libglog.a(libglog_la-logging.o): relocation R_X86_64_32S against `_ZTVN6google4base6LoggerE' can not be used when making a shared object; recompile with -fPIC libs/X86_64/libglog.a: could not read symbols: Bad value collect2: error: ld returned 1 exit status
只能安裝錯誤提供的方法重現編譯libglog.a了,然后,替換了libglog.
CXXFLAGS="-O3 -fPIC" ./configure --prefix=glog-0.3.3/install
參考:http://zrj.me/archives/1066