Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top