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, to call the function you need the dynamic library file separately.
C++ STATIC LIBRARY
Library object file(.o type file) are linked to the .exe(executionable) file by the linker during the compilation. In static linking or when using static library, the function code gets embedded in the .exe file directly. This produces many advantages and drawbacks, we will discuss some in this post.
A Static library has an extension of .a in Linux and .lib in Windows. Go to this link below on how to create a static library.
*Note:If you are using code::blocks in Windows the static library will have .a extension, however, Microsoft Visual Studio will produce .lib file as a static library.
i) Static library are always faster than their counterpart dynamic library. Since the function’s 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 the .exe(executionable) file is bigger when using static library. 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) 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.
C++ DYNAMIC LIBRARY
Dynamic means “run-time“, so program using dynamic library will link their file during run-time. Since it is linked during the run-time, the library code is not embedded in the .exe file. So in order to call the function from the library you need to place the library file in the approprite place.
Like the static library, it also has some advantages and disadvantages, which we will discuss below.
Dynamic library has an extension of .so in Linux and .dll in Windows.
Link : Create dynamic library 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 program can be run beside other programs without overloading the RAM. Simply put, the dynamic library allows creating a mutual respect among the programs.
ii) If 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 programs involve certain steps, at first, the address of the function and the argument type is pushed onto the 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 is called. These steps although requires less time, but supposing if hundreds of function are called we cannot neglect the overhead caused by this.
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 and thus occupying larger space in hard drive.
Static and dynamic library have both advantages and disadvantages. Today’s software mainly uses the dynamic library in their programs, this makes easier code maintenance/modification, and if you need extra features in your software it can be easily integrated 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 use static library to take advantage of their beneficial properties. I recommend using both static and dynamic libraries in your applications.
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.