【问题标题】:Cannot link boost library to shared library无法将 boost 库链接到共享库
【发布时间】:2017-04-12 12:10:37
【问题描述】:

将 boost 1.63.0 库 libboost_regex-mt.a 链接到共享库时,出现错误:

/usr/bin/ld: /usr/local/lib/libboost_regex-mt.a(instances.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libboost_regex-mt.a: could not read symbols: Bad value

我实际上已经使用以下命令编译了带有 -fPIC 的 boost(另见 here):

./bjam '-sBUILD=<cxxflags>-fPIC <linkflags>-fPIC' --without-mpi --without-python --without-iostreams --layout=tagged link=shared,static

在进行 bjam 调试构建时,似乎 bjam 似乎只支持某些源文件的 -fPIC,其他文件(包括 regex 库中的 instances.cpp)在没有它的情况下编译:

gcc.compile.c++ bin.v2/libs/regex/build/gcc-4.8/release/link-static/threading-multi/instances.o

    "g++"  -ftemplate-depth-128 -O3 -finline-functions -Wno-inline -Wall -pedantic -pthread -m64  -DBOOST_ALL_NO_LIB=1 -DNDEBUG  -I"." -c -o "bin.v2/libs/regex/build/gcc-4.8/release/link-static/threading-multi/instances.o" "libs/regex/build/../src/instances.cpp"

如何强制 bjam 对所有文件使用 -fPIC 标志?

【问题讨论】:

    标签: c++ linux gcc boost


    【解决方案1】:

    构建 boost 1.63,共享库:

    ./b2 --without-mpi --without-python --without-iostreams --layout=tagged link=shared runtime-link=shared link=static install
    

    线程=多:

    ./b2 --without-mpi --without-python --without-iostreams --layout=tagged threading=multi link=shared runtime-link=shared link=static install
    

    “正则表达式”结果:ls *regex*

    libboost_regex.a
    libboost_regex-mt.a
    libboost_regex-mt.so@
    libboost_regex-mt.so.1.63.0*
    libboost_regex.so@
    libboost_regex.so.1.63.0*
    

    【讨论】:

    • threading_multi 命令仍然在没有 -fPIC 的情况下构建,例如"g++" -ftemplate-depth-128 -O3 -finline-functions -Wno-inline -Wall -pedantic -pthread -m64 -DBOOST_ALL_NO_LIB=1 -DNDEBUG -I"." -c -o "bin.v2/libs/regex/build/gcc-4.8/release/link-static/threading-multi/instances.o" "libs/regex/build/../src/instances.cpp"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多