Static Linking is the technique of
linking the libraries with the main program while creating the executable of the program.
Dynamic Linking is the technique of linking the libraries with the main program during run-time of the program.
Static Libraries vs Dynamic Libraries
Both of these have their own advantages and disadvantages which have been discussed below :
Advantages of Dynamic Linking over Static Linking
(i) Efficient sharing of code among different programs :
Dynamic linking allows several programs to share the same code instead of having individual copies of the code, thus saving a lot of storage space as well as physical memory (during execution). On the other hand, static libraries cause a wastage of both of these resources because every program has to possess its own copy of the library.
(ii) Ease of updating programs (creating new versions) :
Dynamic linking makes updating programs very simple and resource-efficient. While updating a program, you only have to download the new DLLs and remove the old ones.
On the other hand, static linking makes updating programs a cumbersome task, apart from causing a tremendous wastage of resources.because every time a particular library was updated, the whole program would have to be re-linked. This would result in a new and bulky executable and one would have to remove the previous version entirely and then install the new version from scratch.
Just imagine the scenario where you would have to download several gigabytes of data every month simply for updating a couple of programs.
(iii) Multiple programming language support :
Dynamic libraries can also be shared among programs which have been created using different programming languages. For example, a DLL created for Visual C++ can also be used by Visual Basic. On the other hand, static libraries do not provide this facility because they cannot be shared.
Disadvantages of Dynamic Linking
(i) DLL Hell :
Dynamic libraries aren’t integrated into the programs which use them. Though this leads to sharing of code, it also means that the programs which require these libraries aren’t self-sufficient. In other words, a program requiring a particular DLL wouldn’t work if that DLL is unavailable or incompatible with that version of the program.
Say we have two programs named abc.exe and xyz.exe, both sharing a a library named abcxyz.dll.
Now, suppose abcxyz.dll was updated to enhance the functionality of abc.exe. However, after the update, it became incompatible with xyz.exe.
In this situation, we have two choices :
a) Create another version of abcxyz.dll which works with both programs.
b) Revert to the former version of abcxyz.dll.
As we see, both of these options are inappropriate. This problem can be solved only if we use static libraries instead of dynamic libraries. In that case, any change in the library of one program would have no impact on the other program.
(ii) Unauthorised use of code :
Since dynamic libraries are shareable, it may result in unauthorised sharing of libraries.
For example, Programmer A creates some DLLs and packages them with his programs. Another Programmer B may use these DLLs without A’s permission.
In case of static libraries, there is no risk of unauthorised use since they are contained inside a program and cannot be shared.
(iii) Software Piracy :
Since dynamic libraries exist outside programs, they can be modified easily in order to tamper with the modules that ensure software protection and prevent piracy. In static libraries, there is no such problem.