A library contains some sort of code which is pre compiled and could be used in any program for some specific functionality or feature. It obviously has a lot of advantages. In fact it save you much time by reusing work someone else has already done and be more confident that it has fewer bugs
On the basis of execution and storage of this code, there are 2 types of libraries — Static Libraries and Dynamic Libraries.
In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.
Creating a Static Library file
You have to create the header file with all the prototypes function that you will use, then you have to crear all the .c files with the functions
In order to create a static library using GCC we need to compile our library code into an object file
Here in the above command , all the .c extension files( C files) in the current working directory have been converted in to their respective object files.
After an archive is created, or modified, we have to index it with command : ranlib name_of_file.a
A Dynamic Library is a collection of object files that can be linked to any program at run-time by inserting the location of the function in memory to the executable. This provides a way for code to be used even though it could be loaded anywhere in memory.
How to create a dynamic library?
You have to convert all the .c files in object files using the command :
And then to create the library you have to use the command :
The program needs to know where to look for library files, we must add that location to the environmental variable in this way.
Difference between static and dynamic library
Dynamic libraries are much different than Static Libraries they keep the size of executables low. In fact they provide a way to use code that’s anywhere in memory, if the code is updated, you don’t need to recompile. Just because the code was updated, doesn’t necessarily mean it’s location has been changed, so the library still knows where to direct the program. This means that updating code is much simpler and requires less time.