我需要静态库来静态链接吗?
在“C”、Linux 上,
我是否需要静态库来静态链接,或者我拥有的共享库就足够了? 如果没有,为什么不呢? (它们不包含相同的数据吗?)
On 'C', Linux,
Do I need static libraries to statically link, or the shared ones I have suffice?
If not, why not? (Don't they contain the same data?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您需要静态库来构建静态链接的可执行文件。
静态库是编译对象的捆绑包。 当您静态链接到库时,它实际上与获取该库的编译结果、将它们解压到当前项目中并像使用您自己的对象一样使用它们相同。
动态库已经链接。 这意味着诸如搬迁之类的一些信息已经被修复并被丢弃。
此外,动态库必须编译为位置无关的代码。 这不是对静态库的限制,并且会导致一些常见平台(例如 x86)上的性能显着差异。
存在诸如 ELF Statifier 之类的工具,它们试图将动态链接库捆绑到动态链接的可执行文件中,但它是在所有情况下都很难生成正确的工作结果。
Yes, you need static libraries to build a statically linked executable.
Static libraries are bundles of compiled objects. When you statically link with to library, it is effectively the same as taking the compilation results of that library, unpacking them in your current project, and using them as if they were your own objects.
Dynamic libraries are already linked. This means that some information like relocations have already been fixed up and thrown out.
Additionally, dynamic libraries must be compiled as position-independent code. This is not a restriction on static libraries, and results in a significant difference in performance on some common platforms (like x86).
There exist tools like ELF Statifier which attempt to bundle dynamically-linked libraries into a dynamically-linked executable, but it is very difficult to generate a correctly-working result in all circumstances.
没有静态编译,只有静态链接。 为此,您需要静态库。 静态链接和动态链接之间的区别在于,前者的名称在链接时(编译时之后)解析,而后者的名称在程序开始运行时解析。
静态和动态库可能包含也可能不包含相同的信息,具体取决于许多因素。 静态链接还是动态链接代码的决定非常重要,并且通常会影响应用程序架构。
There is no such thing as static compilation, only static linking. And for that, you need static libraries. The difference between static and dynamic linking is that with the former, names are resolved at link-time (just after compile-time), wheras with the latter, they are resolved just as the program starts running.
Static and dynamic libraries may or may not contain the same information, depending on lots of factors. The decision on whether to statically or dynamically link your code is an important one, and will often influence application architecture.
链接到静态链接程序的所有库都必须是静态变体。 虽然动态(libfoo.so)和静态(libfoo.a)库具有相同的功能,但它们是不同的格式文件,因此您需要与您的程序匹配的类型。
All libraries you link into a statically linked program must be the static variant. While the dynamic (libfoo.so) and static (libfoo.a) libraries have the same functions in them, they are different format files and so you need the matching type for your program.
另一个选择是 Ermine (http://magicErmine.com)
它就像 statifier,但能够处理内存随机化。
Another option is Ermine (http://magicErmine.com)
It's like statifier, but able to deal with memory randomization.