我正在尝试将静态库链接到共享库,我收到了以下错误
/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中,但也没有解决问题
我按照建议here尝试了 -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
创建liblog4cplus.a:
-
unzip log4cplus-1.1.0.zip
-
./configure --enable-static=yes --enable-threads=yes
-
vi Makefile
并将-fPIC添加到CXXFLAGS和CFLAGS -
make
然后编译我的共享库:
-
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
3 回答
假设您正在生成一个共享库,很可能会发生的是您使用的
liblog4cplus.a
的变体未使用-fPIC
编译 . 在linux中,您可以通过从静态库和checking their relocations中提取目标文件来确认:如果输出为空,则静态库不是与位置无关的,不能用于生成共享对象 .
由于静态库包含已编译的目标代码,因此提供-fPIC标志将无济于事 .
你需要得到一个用
-fPIC
编译的liblog4cplus.a
版本,并使用那个版本 .我在安装需要CCD lib(libccd)的FCL时遇到类似的错误,如下所示:
我发现有两个名为“libccd.a”的不同文件:
/usr/local/lib/libccd.a
/usr/local/lib/x86_64-linux-gnu/libccd.a
我通过删除第一个文件解决了这个问题 .
重定位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