将静态库链接到共享库 [英] Linking a Static library into a shared library
问题描述
我已经构建了Poco C ++库的静态库(.a文件)我想静态链接到我的共享库。
但它不起作用。
我有
-Wl,-whole-archive -lPocoFoundation -Wl,-no-whole-存档
g ++抱怨以下错误讯息:
重新定位在创建共享对象时,无法使用针对'-ZTVN4Poco15ArchiveStrategyE'的R_X86_64_32S;使用-fPIC重新编译
有人可以帮忙吗?
按照编译器的建议: 使用 -fPIC
重新编译
解释:共享对象有要求,其代码中的地址不依赖于地址空间中二进制映像的内存布局。静态链接的代码不受此限制,虚拟地址空间中的所有地址在链接时间都是已知的,因此代码不需要处理编译时未确定的位置。
-fPIC
编译器标志可以编译 P I I n独立 C 颂歌(PIC)。您试图链接的静态库不会编译为PIC,这就是连接器抱怨的原因。解决的办法是重新编译启用PIC的静态库。 附注:PIC也是Address空间布局随机化(ASLR)是一种安全措施,其目标是让脆弱程序的开发变得更加困难。
I am trying to create a shared library using g++ 5.4.0 on Ubuntu Linux.
I have built static libs (.a files) of the Poco C++ library and I want to statically link those into my shared library.
But it is not working.
I have added the following string to my build script:
-Wl,-whole-archive -lPocoFoundation -Wl,-no-whole-archive
g++ complains with the following error message:
relocation R_X86_64_32S against '-ZTVN4Poco15ArchiveStrategyE' can not be used when making a shared object; recompile with -fPIC
Can someone help?
Do what the compiler suggests: Recompile with -fPIC
Explanation: Shared Objects have the requirement, that the addresses in their code do not depend on the memory layout of the binary image in address space. Statically linked code is not bound by this, all addresses in virtual address space are known at link time and hence the code is not required to cope with locations being not determined at compile time.
The -fPIC
compiler flag enables compilation of Position Independent Code (PIC). The static libraries you're trying to link were not compiled as PIC that's why the linker complains. The solution is to recompile the static library with PIC enabled.
On a side note: PIC is also a fundamental for Address Space Layout Randomization (ASLR), a security measure with the goal of making the exploitation of vulnerable programs harder.
这篇关于将静态库链接到共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!