What is the difference between static and dynamic linkage?

Asked

Viewed 2,381 times

14

Recently, searching why small Go codes have a much larger executable than the same C-generated code, I read an answer stating that the reason is because Go use linkage static, unlike C, which uses linkage dynamics.

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

2 answers

16


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

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

To dynamics 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 do not need to know the details of what is in each part. Every part is linked separately. It may be until they are from different vendors that only offer an executable access API for dynamic loading (the way to communicate).

Advantages and disadvantages of dynamics

Several techniques can be used for this but the most common is to have the Dlls (Dynamic-Link Library) or Sos (Shared Object) that are executable 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 to replace parts independently has become the main advantage of the linkage dynamics.

Note that if you make an executable and several Dlls, the total size of the executable is larger, not only on disk (to transmit in network), but also in memory. It’s not much, but it’s bigger. It would only make up for it if the computer already has most of these Dlls.

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

Advantage and disadvantages of static

Personally, I prefer static as far as it goes. 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, does not allow this system of plugin (almost always do not need this flexibility, less still need to reuse parts of executable).

It also avoids some indirect and other unnecessary techniques in many cases. An example is create getters and setters because something can be dynamically charged. In linkage Static guaranteed this technique does not help much.

Some will say that it does not load the entire executable if it is dynamic, but this occurs with static as well. The operating system is smart and loads only the necessary pages. This is not a drawback of static.

Completion

You can read more on the subject at What’s the difference between DLL and lib?. And also Performance difference between static and shared library.

11

Static linkage means that the libraries required to run the program are embedded in the executable file itself. That’s Inker, hence the name.

In dynamic linkage libraries remain outside the executable, and are loaded "dynamically" at the time of execution.

A drawback of static linkage you’ve already discovered: executable size increases. Other drawbacks:

  • if a library has problems, such as needing a security update as is so common nowadays, it is not enough to update the library, it is necessary to update all programs that statically linked this library;

  • the program occupies more space in memory because, if several programs statically linked to the same library, they cannot share the memory pages of that library, while this is possible in dynamic linkage.

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 addresses of library functions are solved by Linker.

There is a certain growth trend in static linkage; 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 periodic updating culture of the programs has been created.

In macos the approach is hybrid: the application is a folder and all the necessary dynamic libraries stay inside, which also avoids the "Hell DLL".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.