Static and dynamic libraries In C


In C programming, we use the sqrt function to calculate the square of a number without having to develop the function ourselves. We don’t need to rewrite the sqrt function each time we want to use it because his code already exists in math.h library, we can just call the math.h library and use it. So what exactly are libraries in programming? and Why do we use them?
These and other questions will be addressed in the following sections.
What Is A "C" Library?
A library in C is a collection of precompiled object files that are grouped into a single file. Libraries are provided to reuse code across multiple programs without having to rewrite it each time. C libraries contain functions, data structures, and constants designed to be used.
To use a library in C programming, we include its header file, such as math.h, at the beginning of the program and call its functions in our code.
Why do we use libraries?
In the early days of programming C, there were no functions for basic operations like reading from the keyboard or printing to the screen. Developers had to create their own libraries to handle common tasks such as handling strings and executing math operations. However, in 1989, the ANSI developed the C89 Standard Specification, which introduces a Standard Library containing prewritten code for many common tasks. Additional features were added to the library with the C99 Standard in 1999, including support for complex numbers and new data types.
Using C library functions in programming offers several advantages, such as:
Saving time and effort: Instead of writing code from scratch, developers can use the existing library functions.
Increasing efficiency: C library functions are written by experts, so they are optimized for efficiency and performance.
Standardization: They are consistently defined and implemented across different platforms and operating systems.
Reducing errors: They have been thoroughly testes
Modularity: Library functions allow developers to break down their code into smaller, modular components, making it easier to maintain and save time.
Static Libraries vs. Dynamic Libraries
Static libraries and dynamic libraries are both ways of packaging code, but they differ in how they are linked to the programs that use them:
Static libraries
Static libraries (or archives) are compiled code that is linked directly into an executable program at compile time. This means that the resulting program contains all the code from the library
Dynamic libraries
Dynamic libraries are libraries that are loaded into memory at runtime when a program is executed, unlike static libraries which are linked to the executable at compile time. They can be used by multiple programs simultaneously.
Shared (dynamic) libraries are linked into the program in two stages:
During compile time: the linker verifies that all the required symbols (functions, variables…) are either linked into the program or in a shared library. Dynamic library objects are not inserted into an executable file.
When the program is started: a program called a dynamic loader loads shared libraries used by the program to memory and attaches them to the program's copy.
REFERENCES
[1] WEBOPEDIA. Library. Link: https://www.webopedia.com/TERM/L/library.html
[2] Building And Using Static And Shared "C" Libraries. Link: https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html
[3] C static libraries. Link: https://medium.com/@704/static-libraries-in-c-378e837c6a52
Subscribe to my newsletter
Read articles from Fatima directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
