boost 原始碼編譯和安裝 -fPIC
1.資源
系統:centos 7
boost原始碼:使用的是1.54版本的 boost_1_54_0.tar.gz,具體自己去boost官網下載
2.解壓boost
tar -zxvf boost_1_54_0.tar.gz
注意:由於boost 預設編譯的靜態庫是沒有-fPIC選項,編譯引數也沒有可選的項來新增-fPIC
如果按預設提供的引數選項編譯,那麼在編寫動態庫呼叫boost中的靜態庫時會報錯,
如:libboost_serialization.a(basic_iarchive.o): relocation R_X86_64_32S against `_ZTVN5boost7archive6detail14basic_iarchiveE' can not be used when making a shared object; recompile with -fPIC
3. 解決動態庫呼叫boost靜態庫時報錯 -fPIC的方法
cd boost_1_54_0/tools/build/v2/tools
編輯gcc.jam檔案
vim gcc.jam
找到內容
rule setup-fpic ( targets * : sources * : properties * )
{
local link = [ feature.get-values link : $(properties) ] ;
if $(link) = shared
{
local target = [ feature.get-values target-os : $(properties) ] ;
# This logic will add -fPIC for all compilations:
#
# lib a : a.cpp b ;
# obj b : b.cpp ;
# exe c : c.cpp a d ;
# obj d : d.cpp ;
#
# This all is fine, except that 'd' will be compiled with -fPIC even
# though it is not needed, as 'd' is used only in exe. However, it is
# hard to detect where a target is going to be used. Alternatively, we
# can set -fPIC only when main target type is LIB but than 'b' would be
# compiled without -fPIC which would lead to link errors on x86-64. So,
# compile everything with -fPIC.
#
# Yet another alternative would be to create a propagated <sharedable>
# feature and set it when building shared libraries, but that would be
# hard to implement and would increase the target path length even more.
# On Windows, fPIC is the default, and specifying -fPIC explicitly leads
# to a warning.
if ! $(target) in cygwin windows
{
OPTIONS on $(targets) += -fPIC ;
}
}
}
可以看出編譯動態庫時,並且不是window 會新增-fPIC, 那麼現在需要編譯靜態庫也新增 -fPIC怎麼做呢? 如下
將if $(link) = shared 改為 if $(link) = shared || $(link) = static 即可, 儲存退出
開始編譯boost
1. 在boost_1_54_0目錄下,執行命令: ./bootstrap.sh
2. 同時編譯release版本的動態庫和靜態庫, 執行命令:./b2 --build-type=minimal stage
3. 安裝到指定目錄,如:/home/xiaodj/boost 執行命令:./b2 --prefix=/home/xiaodj/boost install
完成之後在/home/xiaodj/boost中有include和lib, 之後動態庫呼叫boost中的靜態庫正常。
歡迎大牛們提出寶貴意見,共同進步!!!