What is the difference between static and dynamic linking?

8

Recently, researching why small codes in Go have a much larger executable than the same C-generated code, I've read one response stating that the reason is for Go to use linking static , unlike C, which uses linking dynamic .

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

    
asked by anonymous 26.08.2016 / 22:25

2 answers

11

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 dynamic linking (which I know you do not have), nothing prevents it from having.

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

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

Advantages and Disadvantages of Dynamics

Many techniques can be used for this, but the most common is to have Dynamic-Link Library (DLLs) or Shared Object OSs that are executables prepared 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 very desirable advantages. Today this is less important and the ability to replace parts independently has become the main advantage of dynamic linking.

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

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

Advantage and disadvantages of static

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

But it's less flexible, does not allow this plugin system. I almost always do not need this flexibility. Still less need to reuse parts of the executable.

It also allows you to avoid some indirections and other unnecessary techniques in many cases. An example is create getters and setters because something can be loaded dynamically . In guaranteed linking 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 required pages. This is not a disadvantage of static

Conclusion

You can read more about this in What's the difference between DLL and lib? . And also Performance difference between static and shared library

    
26.08.2016 / 22:38
5

Static linking means that the libraries needed to run the program are included in the executable file itself. Who does this is the linker, hence the name.

In dynamic linking libraries remain outside the executable, and are resolved at runtime.

A disadvantage of static linkage you've already discovered: the size of the executable increases. Other disadvantages:

  • If a library has problems, for example needing a security update as it is so common nowadays, it is not enough to update the library, it is necessary to update every executable that links statically; >

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

Now the advantages:

    A library update can not break the program (it avoids the Windows "DLL hell", where different programs depend on different versions of the same DLL to work right);

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

  • There is a small speed advantage (2% to 3%) in static linking because library function addresses are resolved by the linker;

26.08.2016 / 22:37