C++ &引用;重新安置R_X86_64_32S以防;链接错误
我正在尝试将静态库链接到共享库,出现以下错误 /usr/bin/ld: ../../../libraries/log4cplus/liblog4cplus.a(fileappender.o): relocation R_X86_64_32S against `a local symbol' can not be used when making a shared object; recompile with -fPIC ../../../libraries/log4cplus/liblog4cplus.a: could not read symbols: Bad value collect2: ld returned 1 exit status 创建liblog4cplus.a:C++ &引用;重新安置R_X86_64_32S以防;链接错误,c++,linux,shared-libraries,static-libraries,C++,Linux,Shared Libraries,Static Libraries,我正在尝试将静态库链接到共享库,出现以下错误 /usr/bin/ld: ../../../libraries/log4cplus/liblog4cplus.a(fileappender.o): relocation R_X86_64_32S against `a local symbol' can not be used when making a shared object; recompile with -fPIC ../../../libraries/log4cplus/liblog4cp
解压log4cplus-1.1.0.zip
/configure--enable static=yes--enable threads=yes
vi Makefile
并将-fPIC添加到CXXFLAGS和CFLAGS中make
g++-frti-w-c-fPIC-I“Include_目录”myfile.cpp
g++-shared-fPIC-frti-I“Include_Directory”-o mysofile.o-Wl,--整个归档文件”../../../libraries/log4cplus/liblog4cplus.a”-Wl,--没有整个归档文件-ldl
假设您正在生成一个共享库,最可能发生的情况是您正在使用的
liblog4cplus.a
的变体没有使用-fPIC
编译。在linux中,您可以通过从静态库中提取对象文件并执行以下操作来确认这一点:
如果输出为空,则静态库不是位置独立的,不能用于生成共享对象
由于静态库包含已编译的目标代码,因此提供-fPIC标志没有帮助
您需要获得使用
-fPIC
编译的liblog4cplus.a
版本,并改用该版本。我在安装需要CCD库(libccd)的FCL时遇到类似错误,如下所示:
/usr/bin/ld:/usr/local/lib/libccd.a(ccd.o):创建共享对象时,不能使用针对“本地符号”的重新定位R_X86_64_32S;用-fPIC重新编译
我发现有两个不同的文件名为“libccd.a”:
我通过删除第一个文件解决了这个问题 针对未定义符号重新定位R_X86_64_PC32,通常在LDFLAGS设置为强化而CFLAGS未设置时发生。
可能只是用户错误:
如果在链接时使用-specs=/usr/lib/rpm/redhat/redhat-hardend-ld, 您还需要在编译时使用-specs=/usr/lib/rpm/redhat/redhat-hardend-cc1,并且当您同时编译和链接时,您需要两者,或者删除-specs=/usr/lib/rpm/redhat/redhat-hardend-ld。 常见修复:
我在尝试将静态编译对象链接到linux共享对象时也遇到了类似的问题:
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /3rdparty/fontconfig/lib/linux-x86_64/libfontconfig.a(fccfg.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /3rdparty/expat/lib/linux-x86_64/libexpat.a(xmlparse.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
[...]
这与我已经在传递
-fPIC
标志的事实相反,尽管CFLAGS
变量和其他编译器/链接器变体(clang/lld)在相同的构建配置下工作得非常好。结果是,这些依赖项通过卑鄙的autoconf
脚本控制位置无关的代码设置,并且在linux gcc/ld组合上的构建配置期间需要使用pic开关,其缺失可能会覆盖CFLAGS
中的相同设置。将开关传递到configure
script,将使用-fPIC在CMAKE\u CXX\u标志
和CMAKE\u C\u标志
的末尾添加-fPIC
正确编译依赖项
例如:
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall --std=c++11 -O3 -fPIC" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -fPIC" )
这解决了我的问题。您能粘贴完整的命令行吗?如果您明确指出“将静态库链接到共享库”的含义,这也会很好。您是否正在从静态库生成共享库?@MichaelFoukarakis不太适合未来的提问者:有时这仅仅是由于发行版更改而发生的,可以通过快速清理构建目录来修复。Hi fons可能重复,感谢您的回复,并对迟来的回复表示抱歉,你的回答帮助我找出了问题所在。
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /3rdparty/fontconfig/lib/linux-x86_64/libfontconfig.a(fccfg.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /3rdparty/expat/lib/linux-x86_64/libexpat.a(xmlparse.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
[...]
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall --std=c++11 -O3 -fPIC" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -fPIC" )