Static and Dynamic libraries
As we make computer programs, we realize that some parts of the code are used in many of them.
Static and Dynamic libraries
Photo by Jaredd Craig on Unsplash
As we make computer programs, we realize that some parts of the code are used in many of them.
It would be great to be able to put those functions in a separate directory from the actual programs and have them already compiled, so that we can use them whenever we want. The huge advantages of this are:
- Not having to rewrite code (or copy-paste).
- We will save ourselves the time of compiling that code that is already compiled each time. In addition, we already know that while we make a program, we test and correct it, it is necessary to compile between many and “more many” times.
- Already compiled code will be tested and trusted. Not the first times, but yes when we have already used it in 200 different programs and we have been correcting the errors.
A static library is a library that is “copied” into our program when we compile it. Once we have the executable of our program, the library is useless (that is, it is used for other future projects). We could delete it and our program would still work, since it has a copy of everything it needs. Only that part of the library that is needed is copied. For example, if the library has two functions and our program calls only one, only that function is copied.
A dynamic library is NOT copied into our program when it is compiled. When we have our executable and we are running it, every time the code needs something from the library, it will go to the library. If we delete the library, our program will give an error that it cannot be found.
Static VS Dynamic
- A program compiled with static libraries is larger, since everything it needs is copied.
- A program compiled with static libraries can be taken to another computer without the need to take the libraries.
- A program compiled with static libraries is, in principle, faster in execution. When you call a library function, you have it in your code and you don’t have to go read the dynamic library file to find the function and execute it.
- If we change a static library, the executables are not affected. If we change a dynamic, the executables are affected. This is an advantage if we have changed the library to correct a bug (it is automatically corrected in all executables), but it is a drawback if touching that makes us change the executables (for example, we have added one more parameter to a library function , ready-made executables stop working).
HOW USE
To demonstrate how to create the dynamic library we will use the codebase of the project form the previous article:
To start we need to create the object files first with command gcc** -fPIC **-c *.c
As you have noticed, this time we created the object files with the -fPIC flag. This flag stands for Position Independent Code, a characteristic required by shared libraries.
On the next step we will create the library named
gcc -shared -Wl, -soname, libtools.so -o libtools.so *.o
The **-shared key tells the compiler to produce a shared object which can then be linked with other objects to form an executable. `-Wl** flag passes an options to linker with following format-Wl,options`, in case of our example it sets the name of library, as it will be passed to the linker.
2. How to use a dynamic library
Because of its purpose, our library has to be shared dynamically during the linking with other programs, and to make it happen, we have add a path to the library to the **LD_LIBRARY_PATH** environment variable:
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
In case of our example, it is the current working directory, and we can use the **. to add its path. Now the operating system is aware of where to look if some program will request a functionality from the library. Alternatively to configure dynamic linker run-time bindings, we can use the `ldconfig** command with the-n` and **-L** flags.
And the last but not least: a program that wants to rely on the functionality of our library must be compiled in the following manner:
gcc our_sources.c -L. -ltools -o resulted_program
where **-L flag specifies the path to the library, in our case it is current directory, and `-l** flag specifies the name of the library to use. Please note that we didn’t provide thelib` prefix, as well as the **.so** extension: they were resolved by the compiler.
3. Tools to work with dynamic libraries
If we need to find out what functions a library has, we should use the **nm** command:
nm libtools.so 메타데이터
- post_id
- cca2ff44f06a
- slug
- static-and-dynamic-libraries-cca2ff44f06a
- url
- https://medium.com/@coderlancce/static-and-dynamic-libraries-cca2ff44f06a
- canonical_url
- https://medium.com/@coderlancce/static-and-dynamic-libraries-cca2ff44f06a
- author_url
- https://medium.com/@coderlancce
- status
- ok
- fetched_at
- 2026-09-09 08:45:48