我正在尝试将静态库链接到共享库,但出现以下错误
/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:
解压 log4cplus-1.1.0.zip
./configure --enable-static=yes --enable-threads=yes
vi Makefile
并将 -fPIC 添加到 CXXFLAGS 和 CFLAGS制作
然后编译我的共享库:
g++ -frtti -w -c -fPIC -I"Include_Directory"myfile.cpp
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/