C static libraries
Besmira A.
IT Project Manager | Agile & SDLC Expert | Delivering Profitable Global Projects | Remote Work Specialist
In this article we will cover base concepts for C static libraries by these order:
·??????Why use libraries
·??????How does a static library work
·??????How to create them
·??????How to use them
Why use libraries?
In programming, a library is a collection of precompiled routines that a program can use. The routines, sometimes called modules, are stored in object format. Libraries are particularly useful for storing frequently used routines because you do not need to explicitly link them to every program that uses them. Libraries encourage the sharing of code in a modular fashion and ease the distribution of the code. The behavior implemented by a library can be connected to the invoking program at different program lifecycle phases.
How does a static library work?
A static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static. Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time. Any static library function can call a function or procedure in another static library. The linker and loader handle this the same way as for kinds of other object files. Static library files may be linked at run time by a linking loader (e.g., the X11 module loader). However, whether such a process can be called static linking is controversial.
How to create them?
Static libraries can be easily created in C or in C++. These two languages provide storage-class specifiers for indicating external or internal linkage, in addition to providing other features. To create such a library, the exported functions/procedures and other objects variables must be specified for external linkage (i.e. by not using the C static keyword). Static library filenames usually have ".a" extension on Unix-like systems and ".lib" extension on Microsoft Windows.
Steps to create a static library Let us create and use a Static Library in UNIX or UNIX like OS.
1. Create a C file that contains functions in your library.
/* Filename: lib_mylib.c */
#include <stdio.h>
void fun(void)
{
?printf("fun() called from a static library");
}
We have created only one file for simplicity. We can also create multiple files in a library.
2. Create a header file for the library
/* Filename: lib_mylib.h *
void fun(void);/
3. Compile library files.
?gcc -c lib_mylib.c -o lib_mylib.o
4. Create a static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is a static library.
领英推荐
?ar rcs lib_mylib.a lib_mylib.o
5. Now our static library is ready to use. At this point, we could just copy lib_mylib.a somewhere else to use it. For demo purposes, let us keep the library in the current directory.
How to use them?
Let us create a driver program that uses above created static library.
1. Create a C file with main function
/* filename: driver.c?*
#include "lib_mylib.h"
void main()
{
?fun();
}/
2. Compile the driver program.
gcc -c driver.c -o driver.o
3. Link the compiled driver program to the static library. Note that -L. is used to tell that the static library is in the current folder (See this for details of -L and -l options).
gcc -o driver driver.o -L. -l_mylib
4. Run the driver program
./driver
fun() called from a static library
To conclude below are some important points about static libraries.
1. For a static library, the actual code is extracted from the library by the linker and used to build the final executable at the point you compile/build your application.
2. Each process gets its own copy of the code and data. Whereas in the case of dynamic libraries it is only code shared, data is specific to each process. For static libraries, memory footprints are larger. For example, if all the window system tools were statically linked, several tens of megabytes of RAM would be wasted for a typical user, and the user would be slowed down by a lot of paging.
3. Since library code is connected at compile-time, the final executable has no dependencies on the library at the run time i.e. no additional run-time loading costs, it means that you don’t need to carry along a copy of the library that is being used and you have everything under your control and there is no dependency.
4. In static libraries, once everything is bundled into your application, you don’t have to worry that the client will have the right library (and version) available on their system.
5. One drawback of static libraries is, for any change(up-gradation) in the static libraries, you have to recompile the main program every time.
6. One major advantage of static libraries being preferred even now “is speed”. There will be no dynamic querying of symbols in static libraries. Many production line software uses static libraries even today.
References:
https://en.wikipedia.org/wiki/Library_(computing)
https://www.quora.com/What-does-library-mean-in-the-case-of-programming-languages#:~:text=In%20programming%2C%20a%20library%20is%20a%20collection%20of%20precompiled%20routines,every%20program%20that%20uses%20them.https://en.wikipedia.org/wiki/Static_library
https://www.geeksforgeeks.org/static-vs-dynamic-libraries/https://www.google.com/imgres?imgurl=https%3A%2F%2Fmiro.medium.com%2Fmax%2F960%2F1*659sBV_UsXpn2G0BUsKW7Q.jpeg&imgrefurl=https%3A%2F%2Fmedium.com%2F%401124_15114%2Fc-static-libraries-1391377a04d1&tbnid=pNZfwZk-ZHIHWM&vet=12ahUKEwiu3PzbuaL2AhURiv0HHXnCDikQMygAegUIARC6AQ..i&docid=kAXsoeuAUhHENM&w=960&h=720&q=static%20libraries%20c&ved=2ahUKEwiu3PzbuaL2AhURiv0HHXnCDikQMygAegUIARC6A