c++ - "relocation R_X86_64_32S against "链接错误

标签 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/liblog4cplus.a: could not read symbols: Bad value
collect2: ld returned 1 exit status

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

我按照建议尝试了 -whole-archive 标志 here但没有成功。

 
/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

创建 liblog4cplus.a:

  1. 解压 log4cplus-1.1.0.zip
  2. ./configure --enable-static=yes --enable-threads=yes
  3. vi Makefile 并将 -fPIC 添加到 CXXFLAGS 和 CFLAGS
  4. 制作

然后编译我的共享库:

  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

最佳答案

假设您正在生成一个共享库,很可能发生的情况是您使用的 liblog4cplus.a 的变体不是使用 -fPIC 编译的。在 linux 中,您可以通过从静态库中提取目标文件和 checking their relocations 来确认这一点。 :

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

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

由于静态库包含已编译的目标代码,提供 -fPIcflags将无济于事。

您需要获取一个用 -fPIC 编译的 liblog4cplus.a 版本,然后使用那个版本。

关于c++ - "relocation R_X86_64_32S against "链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19768267/

相关文章:

c++ - 为什么这个 static_assert 只在 OS X 上因 clang 失败而在其他地方失败?

c++ - 如何将 int 对 vector 的默认值设置为空?

c++ - 快速生成变量嵌套for循环的数字组合

linux - Linux中/dev/uinput和/dev/input有什么区别?

c++ - 在 Linux 上使用 G++ 共享库

c++ - 使用几何着色器 (openGL) 在单 channel 中渲染多次

c - 使用 execve() 运行子进程的环境变量

linux - shell脚本ssh命令不起作用

linux - 关于loader如何在运行时定位库的问题

c++ - 在 Qt 应用程序中使用自定义 dll