Interview questions: static linking advantages over dynamic linking

Question:

By default, Linux builds user-space programs as dynamically linked applications. Please describe scenarios where you would prefer changing the default behavior to compile your application as a statically linked one. Please explain why?

Solution:

  • Scenario: the application is a part of a critical software infrastructure that should be run in a limited environment or/and should not require the whole system to be in a non-corrupted state. /sbin/ applications, initramfs applications, init process, application of
    rescue mode. Such applications should not rely on the additional layer of dependencies.
  • Scenario: distribution of the software as a standalone binary/deployment of the application without managing its external dependencies/preventing the risk of DLL Hell (linking with an incompatible version of the library). Ease of use and run for the end user. As a part of the application testing process to prove that during linking process all symbols are resolved correctly.
  • Scenario: performance optimization. Use of profile-guided optimization for whole "user-space stack" of the application; get rid of position independent code (pic).
  • Scenario: dynamic linking is not supported by an environment. Self-descriptive.
  • When I know that only my application will use the library. Well, in such a case there is no reason to make the library shared.

Comments

comments powered by Disqus