Do I need static libraries to statically link?

2019-01-24 08:12发布

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?)

4条回答
Emotional °昔
2楼-- · 2019-01-24 08:56

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.

查看更多
何必那么认真
3楼-- · 2019-01-24 08:58

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.

查看更多
Animai°情兽
4楼-- · 2019-01-24 09:04

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.

查看更多
贪生不怕死
5楼-- · 2019-01-24 09:04

Another option is Ermine (http://magicErmine.com) It's like statifier, but able to deal with memory randomization.

查看更多
登录 后发表回答