What is the difference between static and dynamic linkage?

Recently, researching why small codes in Go have a much larger executable than the same code generated in C, I read an answer stating that the reason is because of the Go use static linkage , Unlike C, which uses dynamic linkage .

What exactly do these terms mean? Is there any kind of advantage and/or disadvantage between the two alternatives beyond the final size of the executable?

Author: Maniero, 2016-08-26

2 answers

This was a simplification of the answer, C can use both forms. In thesis Go could also, do not know if the linker of the language is capable today. Unless you have something in the language specification that prevents the dynamic linkage (which I know does not have), nothing prevents it from having if it does not break compatibility.

The static is to create a monolithic executable, so everything you need is already there in the executable.

A dynamic allows parts to be generated separately and added to the executable later (during execution). These parties need to have a way of communicating, a basic protocol, but they don't need to be aware of the details of what's in each party. Each part is linked separately. It can be until they are from different vendors that only offer an executable access API for dynamic loading (such as communicate).

Advantages and disadvantages of dynamics

Several techniques can be used for this but the most common is to have DLLs (Dynamic-Link Library) or SOs (shared Object) which are executables prepared precisely to be loaded together with another main executable.

In the past the size of the executable and the reuse of parts that can be used for various applications were much desired advantages. Today this has less importance and the ability replacing parts independently has become the main advantage of dynamic linkage.

Note that if you make an executable and several DLLs, the total size of the executable becomes larger, not only on disk (for network streaming), but also in memory. It's not much, but it's bigger. It would only compensate if the computer already has a good part of these DLLs.

There are cases that the dynamic linkage is required because of the license (LGPL for example).

Advantage and disadvantages of static

Particularly I prefer the static as far as it gives. It is easier to manage and install, has more load performance (even if minimal) and allows better optimizations (being able to analyze everything is very powerful).

But it is less flexible, it does not allow this system of plugin (almost always I do not need this flexibility, even less I need to reuse parts of the executable).

Also avoids some hints and other techniques unnecessary in many cases. An example is create getters and setters because something can be loaded dynamically. In static linkage guaranteed this technique does not help much.

Some will say it doesn't load the entire executable if it's dynamic, but this happens with static as well. The operating system is smart and loads only the necessary pages. This is not a disadvantage of static.

Conclusion

You can read more about the subject in What is the difference between DLL and lib?. And also performance difference between static and shared library .

 16
Author: Maniero, 2020-11-06 11:53:40

Static linking means that the libraries necessary for the operation of the program are embedded in the executable file itself. Who does this is the linker, hence the name.

In dynamic linking libraries remain outside the executable, and are loaded "dynamically", at runtime.

One drawback of static linking you've already discovered: the size of the executable increases. Other disadvantages:

  • If a library has problems, as for example need a security update as is so common nowadays, it is not enough to update the library, it is necessary to update all the programs that have statically linked this library;

  • The program takes up more memory space because if several programs have statically linked the same library, they cannot share the memory pages of that library, whereas this is possible in dynamic linking.

Now the advantages:

  • A library update that goes wrong does not affect the program. This avoids the" DLL hell " of Windows, where different programs depend on different versions of the same DLL to work right.

  • It is easier to install (deploy) the program in production because there are fewer dependencies, or even no dependencies;

  • There is a small speed advantage (2% to 3%) in a statically linked program, because the functions of the libraries are solved by the linker.

There is a certain growth trend of static linking; for example, programs in Go and Rust link statically. Nowadays the size of the executables is relatively small close to the assets (images, sounds, videos), and the culture of periodic updating of programs has already been created.

On MacOS the approach is hybrid: the application is a folder and all the necessary dynamic libraries are inside, which it also avoids the "DLL hell".

 11
Author: epx, 2020-02-18 21:33:27