You came to the right place if you’re wondering how static and dynamic libraries work in C programs. You must be thinking…
What is a library in Linux?
Why do we use libraries?
How to create a static and dynamic library?
How and why do we use them?
A Library in Linux
A library is a collection of pre-compiled pieces of code called functions. The library contains common functions and together, they form a package called — a library. Functions are blocks of code that get reused throughout the program. Using the pieces of code again in a program saves time. It keeps the programmer from rewriting the code several times. For programmers, libraries provide reusable functions, data structures, classes and so forth.
“For instance, if you are building an application that needs to perform math operations, you don’t have to create a new math function for that, you can simply use existing functions in libraries for that programming language.” (“Understanding Shared Libraries In Linux”)
A library is not executable and that is a key difference from processes and applications. Libraries play their role at run time or compile time. In the C programming language, we have two types of libraries: dynamic libraries and static libraries.
Libraries have object files created by the
“-c” gcc flag and end in
“.o” by convention. They are the result of the output of the compiler and contain function definitions in binary form.
Differences Between Dynamic and Static Libraries
Dynamic or shared libraries occur as separate files outside of the executable files. Thus, it only needs one copy of the library’s files at runtime. At compile time, static libraries stay locked into a program. It contains the file’s programs holding a copy of the library’s files at compile time.
When using a dynamic library, the programmer is referencing that library when it needs to at runtime. For instance, to access the string length function from the
standard input/output header file — you can access it dynamically. It will find the program’s library reference at runtime because of the dynamic loader. It then loads that string length function into memory. Thus, the dynamic library accessibility must be readily available or it becomes powerless.
Advantages and Disadvantages of Dynamic Libraries
- It only needs one copy at runtime. It is dependent on the application and the library being closely available to each other.
- Multiple running applications use the same library without the need of each file having its own copy.
- However, what if the dynamic library becomes corrupt? The executable file may not work because it lives outside of the executable and is vulnerable to breaking.
- They hold smaller files.
- Dynamic libraries are linked at run-time. It does not require recompilation and relinking when the programmer makes a change.
At compile time, applications utilize static libraries. All the copies of the functions get placed into the application file because they are needed to run the process.
Advantages and Disadvantages of Static Libraries
- Static libraries resist vulnerability because it lives inside the executable file.
- The speed at run-time occurs faster because its object code (binary) is in the executable file. Thus, calls made to the functions get executed quicker. Remember, the dynamic library lives outside of the executable, so calls would be made from the outside of the executable.
- Changes made to the files and program require relinking and recompilation.
- File size is much larger.
How do you create a static and dynamic library?
For both libraries, your program should include a
prototype for each of the functions that exist in your library. If using a header file, don’t forget to include the
#include "<header file name>"
To create a dynamic library, write the following command:
gcc -g -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o liball.so
“*.c” — it takes all of the C source files in the current directory and makes a shared library called
-fPIC flag allows the following code to be referenced at any virtual address at runtime. It stands for Position Independent Code.The library does not hold data at fixed addresses because its location in memory will change between programs. Object files get compiled by using the
-shared flag. The
compiler will later identify a library by searching for files beginning with
“lib” and ending with the naming convention,
The program needs the path in order to look for the library files. So, you must type the following command to add that location to the environment variable called
When using the dynamic library, type the following command:
gcc -g -wall -o app app.c liball.so
To create a static library, we need to use the following command:
ar rc liball.a dog.o cat.o bird.o
archiveand it is used to create the static library. All the files ending in
“.o”will be added to the
liball.a libraryand they are the object files in this case.
-rc flagwill do two things: replace and create a new library if it does not exist already. The next step is indexing, to do that, we type:
When using the static library, type the following command:
gcc main.c -L -l<filename>
-L flag accesses the
linker so the libraries can be found in the given directory. Also, it also searches for other locations where the compiler looks for system libraries.
Learn more from the following resources and I hope you have a better understanding of static and dynamic libraries.