FOR FREE CONTENT

Static linking and Dynamic linking

 

Important point: This chapter is very important for examination: NET/GATE/Engineering/Degree course

 

Static linking and Dynamic linking

 

There is basically two types of linking:

(a) Static linking

(b) Dynamic linking

 

(a) Static linking: 

Static linking means link or resolve all unresolved symbols and assign relocatable addresses before load the final object file into the main memory i.e. All linking is done before loading the program in the main memory.

 

Example: C language is doing static linking.

 

Advantage: 

All the linking is done before loading the program into the main memory so, there is no linking required at the time of execution of the program. So, in the case of static linking execution will be faster compare to dynamic linking.

 

Disadvantage:

Because all linking is done before loading the program in main memory, the program size is bigger than dynamic linking at execution time.

 

(b) Dynamic linking:

Dynamic linking means some unresolved symbol that is not solved at linking time, it will be solved after load the object file into main memory i.e. linking will be done at the time of execution when programs are in main memory.

 

Example: In C++ language, we can do static linking or dynamic linking. But in JAVA language has always dynamic linking.

 

Shared library:

Several programs use some common modules of the library and one of these modules already resides in the main memory loaded by some programs which are executable. When any program which is going to load in main memory and these modules are required then they need not load it again because these modules already loaded by others program in main memory. 

 

This commonly shared library module which is needed not loaded several times for each and every program is called the shared library. This shared library is called  Dynamically linked libraries (DLLs). 

 

Then most of the cases shared library files are dynamic linking because most of the programs use common shared libraries from main memory so, there is no provision to link it at linking time. It can be shared at the time of execution from other programs which already present in the main memory. 

 

For example, printf() or scanf() are shared files because there is a high chance that it already loaded at main memory by other programs. So, for the new program, it is not necessary to load it again. 

So, in the case of printf() or scanf() it resolves at execution time not linking time.

 

 

No relocatable address assign for printf() at linking time rather there write a small code called stub or glue code. 

 

# A stub is included in the image for each library routine reference. The stub or glue is a small piece of code that indicates how to locate the appropriate memory-resident library routine or how to load the library if the routine is not already present.

 

When the stub is executed, it checks to see whether the needed routine is already in memory. If it is not, the program loads the routine into memory.