Which is recommended: using static lib vs dynamic lib (shared object)

I am working with an application on Linux. It supports both static and dynamic versions (.so)

In terms of performance, which version should the user use? The application performs computational tasks that require several hours of processor time.

Any other advantage of using one lib over another?

thanks

+4
source share
4 answers

In terms of net performance:

Common objects are compiled as PIC (position-independent code), which theoretically may be slightly less efficient than regular code on some architectures (including x86).

However, I do not think that would make a real difference.

From any other points

Using a shared object, it has too many advantages over a static library, which is simply the best alternative.

+4
source

In terms of performance, the differences are minimal if the dynamic library is not loaded and unloaded frequently.

The only difference is that the dynamic library is loaded when necessary, instead of being embedded (and thus ever present, without loading) the executable.

A dynamic library can also be reused by multiple executables. This is the main reason I have used dynamic libraries in the past.

+3
source

Normally you should use dynamic lib to reduce the size of the binary. There is no runtime penalty, with the exception of running the application, which probably doesn't matter.

+2
source

Typically, statically linked libraries are faster because they do not have the overhead of finding and loading the library, but: for a many-hour program, the difference in performance should be too small to notice.

In any case, the only way to be sure of this is to test it yourself.

+1
source

Source: https://habr.com/ru/post/1300045/


All Articles