Linux “重定位 R_X86_64_32S 反对”链接错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19768267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:15:47  来源:igfitidea点击:

"relocation R_X86_64_32S against " linking Error

c++linuxshared-librariesstatic-libraries

提问by Thiyagarajan

I'm Trying to Link a static Library to a shared library , I'm Getting the Following error

我正在尝试将静态库链接到共享库,但出现以下错误

/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

But this worked on a 32bit machine without any such error. I tried adding The -fPICflags manually to the Makefile that too didn't solve the problem

但这在 32 位机器上运行没有任何此类错误。我尝试将-fPIC标志手动添加到 Makefile 也没有解决问题

I tried the -whole-archiveflag as suggested herebut with no success.

-whole-archive按照此处的建议尝试了该标志,但没有成功。

 
/usr/bin/ld: ../../../libraries/log4cplus/liblog4cplus.a(appenderattachableimpl.o): relocation R_X86_64_32S against `vtable for log4cplus::spi::AppenderAttachable' can not be used when making a shared object; recompile with -fPIC
../../../libraries/log4cplus/liblog4cplus.a(appenderattachableimpl.o): could not read symbols: Bad value
collect2: ld returned 1 exit status

Creation of liblog4cplus.a:

创建 liblog4cplus.a:

  1. unzip log4cplus-1.1.0.zip
  2. ./configure --enable-static=yes --enable-threads=yes
  3. vi Makefileand added -fPIC to CXXFLAGS and CFLAGS
  4. make
  1. unzip log4cplus-1.1.0.zip
  2. ./configure --enable-static=yes --enable-threads=yes
  3. vi Makefile并将 -fPIC 添加到 CXXFLAGS 和 CFLAGS
  4. make

Then for Compiling my shared library:

然后编译我的共享库:

  1. g++ -frtti -w -c -fPIC -I"Include_Directory" myfile.cpp
  2. g++ -shared -fPIC -frtti -I"Include_Directory" -o mysofile.so myfile.o -Wl,--whole-archive "../../../libraries/log4cplus/liblog4cplus.a" -Wl,--no-whole-archive -ldl
  1. g++ -frtti -w -c -fPIC -I"Include_Directory" myfile.cpp
  2. g++ -shared -fPIC -frtti -I"Include_Directory" -o mysofile.so myfile.o -Wl,--whole-archive "../../../libraries/log4cplus/liblog4cplus.a" -Wl,--no-whole-archive -ldl

采纳答案by fons

Assuming you are generating a shared library, most probably what happens is that the variant of liblog4cplus.ayou are using wasn't compiled with -fPIC. In linux, you can confirm this by extracting the object files from the static library and checking their relocations:

假设您正在生成一个共享库,最有可能发生的情况是liblog4cplus.a您使用的变体不是用-fPIC. 在 linux 中,您可以通过从静态库中提取目标文件并检查它们的重定位来确认这一点:

ar -x liblog4cplus.a  
readelf --relocs fileappender.o | egrep '(GOT|PLT|JU?MP_SLOT)'

If the output is empty, then the static library is not position-independent and cannot be used to generate a shared object.

如果输出为空,则静态库不是位置无关的,不能用于生成共享对象。

Since the static library contains object code which was already compiled, providing the -fPIC flag won't help.

由于静态库包含已编译的目标代码,因此提供 -fPIC 标志无济于事。

You need to get ahold of a version of liblog4cplus.acompiled with -fPICand use that one instead.

您需要获得一个已liblog4cplus.a编译的版本-fPIC并改用该版本。

回答by Saeed Mohtasham

I've got a similar error when installing FCL that needs CCD lib(libccd) like this:

在安装需要 CCD lib(libccd) 的 FCL 时,我遇到了类似的错误,如下所示:

/usr/bin/ld: /usr/local/lib/libccd.a(ccd.o): relocation R_X86_64_32S against `a local symbol' can not be used when making a shared object; recompile with -fPIC

/usr/bin/ld: /usr/local/lib/libccd.a(ccd.o): relocation R_X86_64_32S针对`a local symbol'不能在制作共享对象时使用;用 -fPIC 重新编译

I find that there is two different files named "libccd.a" :

我发现有两个不同的文件名为 "libccd.a" :

  1. /usr/local/lib/libccd.a
  2. /usr/local/lib/x86_64-linux-gnu/libccd.a
  1. /usr/local/lib/libccd.a
  2. /usr/local/lib/x86_64-linux-gnu/libccd.a

I solved the problem by removing the first file.

我通过删除第一个文件解决了这个问题。

回答by Sérgio

Relocation R_X86_64_PC32 against undefined symbol , usually happens when LDFLAGS are set with hardening and CFLAGS not .
Maybe just user error:
If you are using -specs=/usr/lib/rpm/redhat/redhat-hardened-ld at link time, you also need to use -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 at compile time, and as you are compiling and linking at the same time, you need either both, or drop the -specs=/usr/lib/rpm/redhat/redhat-hardened-ld . Common fixes :
https://bugzilla.redhat.com/show_bug.cgi?id=1304277#c3
https://github.com/rpmfusion/lxdream/blob/master/lxdream-0.9.1-implicit.patch

针对未定义符号的重定位 R_X86_64_PC32,通常发生在 LDFLAGS 设置为硬化而 CFLAGS 未设置时。
也许只是用户错误:
如果您在链接时使用 -specs=/usr/lib/rpm/redhat/redhat-hardened-ld,您还需要使用 -specs=/usr/lib/rpm/redhat/redhat-hardened -cc1 在编译时,并且当您同时编译和链接时,您需要两者之一,或者删除 -specs=/usr/lib/rpm/redhat/redhat-hardened-ld 。常见修复:
https: //bugzilla.redhat.com/show_bug.cgi ?id = 1304277# c3
https://github.com/rpmfusion/lxdream/blob/master/lxdream-0.9.1-implicit.patch

回答by ceztko

I also had similar problems when trying to link static compiled fontconfigand expatinto a linux shared object:

在尝试将静态编译的fontconfigexpat链接到 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
[...]

This contrary to the fact that I was already passing -fPICflags though CFLAGSvariable, and other compilers/linkers variants (clang/lld) were perfectly working with the same build configuration. It ended up that these dependencies control position-independent code settings through despicable autoconfscripts and need --with-picswitch during build configuration on linux gcc/ld combination, and its lack probably overrides same the setting in CFLAGS. Pass the switch to configurescript and the dependencies will be correctly compiled with -fPIC.

这与我已经通过变量传递-fPIC标志的事实相反CFLAGS,其他编译器/链接器变体 (clang/lld) 可以完美地使用相同的构建配置。最终这些依赖项通过卑鄙的autoconf脚本控制与位置无关的代码设置,并且--with-pic在 linux gcc/ld 组合上的构建配置期间需要切换,并且它的缺失可能会覆盖CFLAGS. 将 switch 传递给configure脚本,依赖项将被正确编译-fPIC