Static Link Library (SLL) vs. Dynamic Link Library (DLL) in C/C++

September 24th, 2009 Leave a Comment

At times, we may ask if we should use SLLs (*.lib, *.a) or DLLs (*.dll, *so) for our projects. Today, let’s discuss a bit on this issue.

When static linking is used, the linker extracts and copies the needed function from the library file into the executable. Only the executable file needs to be distributed to the end user.

As for dynamic linking, the link to a function in a dynamic library is resolved at runtime. Both the executable file and the needed DLLs are loaded into the memory when the program runs. The executable file and the needed DLLs need to be distributed to the end user.

Let’s look at the advantages of using static linking.

  • Static linking includes only the part of the library that is actually needed by the applications while dynamic linking loads the entire library (or a large part of it) into the memory when only a function is needed.
  • There is only one file to load, the executable file, when static linking is used. For dynamic linking, it is necessary to load several files when the program is started.
  • It takes longer time to call a function in a DLL than a SLL because it needs an extra jump through a pointer in an import table.
  • The memory space will become more fragmented when the code is distributed between multiple DLLs as they are always loaded at round memory address divisible by the memory page size. This will make all DLLs contend for the same cache lines which makes code caching and data caching less efficient.

Now, the followings are the advantages of using dynamic linking.

  • Multiple application running simultaneously can share the same DLL without the need to load more than one instance of the DLL into memory. This is useful on servers that run many processes simultaneously.
  • A DLL can be updated to a new version without the need to update the program that calls it.
  • A DLL can be called from programming languages that do not support static linking.
  • A DLL can be useful for making plug-ins that add functionality to an existing program.

As results, when you’re choosing either SLL or DLL to use, weigh the advantages and disadvantages. Identify the one that suits your project or application better.

, ,

Leave a Reply

× nine = 63