Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need static libraries to statically link?

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

like image 749
Liran Orevi Avatar asked Jun 20 '09 17:06

Liran Orevi


People also ask

When should I use static library?

If you have a lot of files, multiple copies of a static library means an increase in the executable file's size. If, however, the benefits of execution time outweigh the need to save space, the static library is the way to go.

What does it mean to statically link a library?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

Why might you choose to link your program statically rather than use a shared library version?

Library references are more efficient because the library procedures are statically linked into the program. Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system.


1 Answers

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.

like image 75
ephemient Avatar answered Sep 21 '22 16:09

ephemient