Dynamic Libraries
What are libraries?
Libraries are a way of condensing code into smaller units. Instead of having multiple files, we can have one single file that contains multiple object files. The way it works is when a C file is compiled, part of the compilation process is the linking phase. During this phase, functions used within the program are checked in a library to see if the code exists or not. There are two types of libraries used in this way. The first is called a static library. The second is called a dynamic library.
How do they work?
A dynamic library is great because you don’t need to recompile or relink the applications using certain functions if they change, as you would need to do in a static library. Shared libraries can also be called by programs written in different programming languages. A disadvantage to a shared library is that your application is dependent on tons of files that aren’t local to your individual program. This means you have to ensure everything’s updated and in sync and that there’s documentation on what was required for installation to run your program.
Advantage/Disadvantage
The good thing about static libraries is that you can hand someone your program and it’s entirely ready to go. There’s nothing the user needs to download or install to make your program work. It’s fully autonomous. The bad thing about static libraries are that it makes your program way bigger, taking up much more space than using a dynamic library would.
How to use them?
To create a dynamic libraries (Linux), you cant type this command:
gcc -g -fPIC *.c -shared -o liball.so
That create you a dynamic libraries
Flags
- g : includes debugging information
- fPIC : “Position Independent code”; this allows for the code to be located at any virtual address at runtime
- shared : creates the shared library with the prefix lib and the suffix .so, standing for “shared object”
Advantage/Disadvantage
The good thing about static libraries (I write an article on how the statics libraries works) is that you can hand someone your program and it’s entirely ready to go. There’s nothing the user needs to download or install to make your program work. It’s fully autonomous. The bad thing about static libraries are that it makes your program way bigger, taking up much more space than using a dynamic library would.