First we need to know what are libraries? Libraries are simply just files that contain functions, they are not executable. Applications are the ones that can execute, and they are the ones that utilize libraries. Commonly used functions are normally in libraries so that they can be called upon when using the executable application.
The first kind of library we will talk about is a static library. These libraries are used during the compilation process (one of the differences from a dynamic library). The code from the static libraries are pretty much copied and moved into the application during compilation process (specifically linking).
Pros and cons: This usually means that our executable application will become bigger when utilizing static libraries, but it also means that the application will have all the functions it needs to run by itself. No need to reference to a function, unlike a dynamic library. An application that uses a static library will also need to be updated if the library itself had some changes, since the application has the library of functions compiled into it, this is another of the drawback of using a static library.
The second kind of library is a dynamic library. These libraries are used outside of compilation, and are called upon while executing the application. As mentioned in the static library, during the linking part of compilation of an application, the code from the library is put into the application. In the case of dynamic library, the address of the library is copied and moved into the application. Any update done to the libraries will reflect on the application, as the application references the library by memory, which makes it more dynamic when updating.
Pros and cons: Application is generally not as big, since you have the address of the library instead of the entire copy of it. However, the application will rely on the library being there when needed, otherwise the application won’t work.
Now we will get into creating dynamic libraries and using them: the command for this is
gcc -g -fPIC -Wall -pedantic -Werror -Wextra *.c -shared -o LIBRARY_NAME.so . The -g flag is for debugging info information, and the -fPIC flag stands for “Position Independent Code”, a requirement for dynamic libraries. Lastly, the -shared flag creates the shared library (
.so for shared object).
Using the created dynamic library: the command is
gcc -g -o APP_NAME FILE.c LIBRARY_NAME.so . This will compile to your named app with your main
.c file and your library name