Static and Dynamic Library in C++

Static and Dynamic library in C and C++
There are two types of library in C++:

Static library:Static library are those library whose functions code get embedded in the .exe file while compiling.

Dynamic library:Dynamic library function code are not embedded in the .exe file so,to call the function you need the dynamic library file separately.


Library object file(.o type) are linked to the .exe(executionable) file by the linker during compilation.In static linking the function code gets embedded in the .exe file directly.This produces many advantages and drawbacks,we will discuss some below. Static library have an extension of .a in Linux and .lib in Windows.Go to this link (Static Library) on how to make a static library with Code::blocks.

*Note:If you are using code::blocks in Windows the static library will have .a extension however,Microsoft Visual Studio will produce .lib file for static library.


i)Static library are always faster than the dynamic library.Since the functions code are found in the .exe file the overhead of calling and transferring the function and it’s argument to the library file and going back to the main program is avoided.

ii)The main program is already compact with the static library while compiling so,the user will always have the right library.The chances of calling a wrong function during run-time while using static library is zero.

iii)If your application uses static library there is no extra need to distribute the library.The main program has all the library functions your client need.



i)The size of an .exe(executionable) file is bigger.Since the library code is inserted into the .exe file directly the size is larger.Larger .exe file size means occupying larger space in RAM.

ii) The another disadvantage of using static linking is,if any changes is made in the library the whole program would have to be recompiled to apply the changes for every process.This means waste of time and perhaps energy too.



Dynamic means “run-time” so,program using dynamic library will link their file during run-time.Since it is linked during run-time the library code is not embedded in the .exe file.So in order to call the function from the library just place the name in the binary file.Like the static library it also has some advantage and disadvantage which,we will discuss below.Dynamic library has an extension of .so in Linux and .dll in Windows.Go to this link (Dynamic library) on how to make a .dll file with Code::blocks.


i)With dynamic library you can make your .exe file small.This provides an advantage for bigger software like OS where lots of system tools need to be run at the same time.Howsoever big the application may be if the .exe file is small the programmed can be run also allowing other program to run without overloading the RAM .Simply put,dynamic library allows to create a mutual respect among the programs.

ii) I f you want to make any changes to the library it can be done easily and only the library has to be recompiled not the whole program.



i)Calling functions in dynamic library is slower.Calling a function in programming involve certain steps,at first address of function and argument type is push onto stack,using that address the execution of the program is transfer to the place where the actual code of the function is present.Also after the function is executed the execution of the program is transferred to the main program at the place after the function was called.These steps although requires less time,supposing,if hundreds of functions( I know this is a minimal case ) is required to be called we cannot neglect the overhead.

ii)Since dynamic library requires calling the function during run-time the function should be made available to the user in a separate file.This means that the whole .dll (for Windows software) file should be transferred with the main program occupying larger space in hard drive.

Static and Dynamic library has both advantages and disadvantages.Today’s software mainly uses dynamic library in their program,this makes easier code maintenance/modification , and if you need extra features in your software it can be easily added using plug-in system.Although,software using dynamic linking occupy larger space in hard drive modern computer provides sufficient amount of space in hard drive so,hard drive space won’t be much of an issue. However,companies still uses static library while taking advantage of their beneficial properties.I recommend using both static and dynamic library in your application.

*Side note

If you are using code::Blocks or any other compiler to compile your C++ programs then the libraries(for example, <iostream> ) are linked using static method. This means the libraries are encoded in the .exe file directly.If you want a proof of this then include many libraries in your program and compile it,the program which uses more library will have a bigger .exe size.