Static vs Shared (Dynamic) Libraries

Static vs Shared (Dynamic) Libraries

What are libraries in software development?

A library is an external compiled code that your application can use.

Libraries are not executables as are programs. Instead, they are invoked by programs for accomplishing a particular task.

A Static Library is statically linked in during the compilation process, into your program's final compiled executable.

Only the functions that are used by the program are actually linked in, not the entire library.

PRO:

  • Since the executable now contains the static library inside itself, when you distribute your application you just send your customer the executable, which contains everything necessary to run the application.
  • Applications that use Static Libraries run faster since the library code is inside the executable, it can just do a fast local jump to access that code.
  • Going to your fridge in another room of your house to get food.

CON:

  • If your application depends on many static libraries, all those static libraries (at least the parts of them that you use) will get linked into your executable, increasing it's size very significantly. Thus the time to download the application will increase.
No alt text provided for this image

A Dynamic Library is dynamically linked into the application, during the running time of the program.

When you compile your executable, the code of the library isn't linked into it.

Instead, the dynamic library exists as a separate file that the application references when it runs.

PRO:

  • Since the executable doesn't contain the code of the dynamic library inside itself, the actual executable is smaller in size, and quicker to download.
  • Another pro to Dynamic Libraries is that they are Shared Libraries. If you have 80 different applications that depend on the dynamic library, they all call the same DLL at run time. They are SHARING the library. You do not have duplication of the library's code, and each executable only contains it's own code.
  • This is why Dynamic Libraries are called Shared Libraries.

CON:

  • If your application relies on 100 different dynamic libraries, then all 100 dynamic library files have to be present on the PC if you want to run your program. So you have to send your customer both the application and the 100 dynamic library files in order for them to run it.
  • Of course, if that dynamic (or shared) library is already installed on the customer's PC, like various standard OS libraries, then you don't need to send it over. Chances are that on Windows, the Win OS DLLs are already installed on the customer's machine.
  • Applications that use Dynamic/Shared Libraries run slower since the code execution has to jump to a whole another file, and then return back to the original executable file again.
  • Going to the store to get food.
No alt text provided for this image


Use Static Libraries if:

  • You need a fast running time.
  • Size of the executable does not matter.
  • There will only be one executable so code duplication is not a problem.
  • The size of the library is small.
  • The (maybe proprietary) library is tightly coupled with your executable (or several related executables), and it will not be used or shared by any other external programs.


Use Dynamic Libraries if:

  • Your application must be small.
  • You don't necessarily care about a fast running time.
  • The library will be used or shared by many unrelated applications.
  • The size of the library is very large.
  • It is a generic library or a system library that is already installed on the customer's machine.


Static Library Files:

  • *.a
  • *.lib      Windows (Visual Studio)

Dynamic Shared Library files:

  • *.so       Linux
  • *.dll       Windows
  • *.dylib   MacOS



To view or add a comment, sign in

More articles by Constantin Rebrov

Others also viewed

Explore content categories