问题背景:应用中引入了boost库,引用库使用-fPIC编译选项 为了能够直接使用静态库进行编译链接
编译错误:libboost_system.a(error_code.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/libboost_system.a: error adding symbols: Bad value
分析处理:
➜ boost_1_62_0 pwd
/root/3rd/boost_1_62_0
➜ boost_1_62_0 vim tools/build/src/tools/gcc.jam
# 将403 修改为 if $(link) = shared || $(link) = static
400 rule setup-fpic ( targets * : sources * : properties * )
401 {
402 local link = [ feature.get-values link : $(properties) ] ;
403 if $(link) = shared
404 {
405 local target = [ feature.get-values target-os : $(properties) ] ;
406
407 # This logic will add -fPIC for all compilations:
408 #
409 # lib a : a.cpp b ;
410 # obj b : b.cpp ;
411 # exe c : c.cpp a d ;
412 # obj d : d.cpp ;
413 #
414 # This all is fine, except that 'd' will be compiled with -fPIC even
415 # though it is not needed, as 'd' is used only in exe. However, it is
416 # hard to detect where a target is going to be used. Alternatively, we
417 # can set -fPIC only when main target type is LIB but than 'b' would be
418 # compiled without -fPIC which would lead to link errors on x86-64. So,
419 # compile everything with -fPIC.
420 #
421 # Yet another alternative would be to create a propagated <sharedable>
422 # feature and set it when building shared libraries, but that would be
423 # hard to implement and would increase the target path length even more.
424
425 # On Windows, fPIC is the default, and specifying -fPIC explicitly leads
426 # to a warning.
427 if ! $(target) in cygwin windows
428 {
429 OPTIONS on $(targets) += -fPIC ;
430 }
431 }
432 }