Brief: This article guide provides an understanding between static and dynamic libraries and thereafter demonstrates how a user can create a static library in Linux via the aid of the Linux ar command.
During the compilation process of a C program, an object code is usually the end result of a successful compiler runtime. Afterward, a linker is invoked to make the library function code available to a user program. The linker accomplishes this objective either by making the library function code copy available to the user object code or by availing the library function code at run-time.
When the linker creates a copy of these re-usable library function codes and passes/avails them to an executable program file, a static library is born. It’s called a static library because the libraries and/or library files are statically linked. Static libraries in Linux are identified by the '.a'
file extension.
Features of a Static Library
Before we create a static library in Linux, we should note the following advantageous features:
- Reduced Size – They are smaller in size in comparison to shared libraries hence advantageous to limited storage space systems.
- Improved Performance – During runtime, shared libraries are associated with the overhead of loading (tracing a shared library location, verifying its version, and mapping it to memory for an executable program to access it). With the static library, all the necessary code and data are readily availed to the executable program.
- Improved Portability – Static libraries’ linked executables are easy to deploy and run since they do not depend on external runtime environments or libraries. They are therefore adaptable to different systems, unlike shared libraries.
- Improved Stability – The executable program’s stability is likely improved since it is bundled with the static libraries which prevent the library from being modified or updated while on a different system environment (a key cause for compatibility issues).
Creating a Static Library in Linux
We will accomplish the creation of our static library by covering the following steps:
Step 1: Create a C File
The first step is to create a C file that will accommodate all the needed library functions.
$ nano mylibrary.c OR $ vi mylibrary.c
We will use the following code for simplicity reasons:
/* Filename: mylibrary.c */ #include <stdio.h> void task(void) { printf("task() called from a static library"); }
Step 2: Create a Library Header File
The next step is to create the library header file:
$ nano mylibrary.h OR $ vi mylibrary.h
The code to use is:
/* Filename: mylibrary.h */ void task(void);
Step 3: Create an Object File
The next step is to compile the above library files and create an object file mylibrary.o using a gcc compiler.
$ gcc -c mylibrary.c -o mylibrary.o
The -c
flag compiles the C library and the -o
flag creates the library object. You can create multiple library objects from different C files.
Step 4: Create a Static Library
Once you have the needed number of library objects, you can proceed to create a static library. Here, we will borrow the functionality of the Linux ar Command.
$ ar rcs mylibrary.a mylibrary.o
The above command creates a static library called mylibrary.a
. You can also include multiple library objects in the following command format:
$ ar rcs mylibrary.a lib1.o lib2.o lib3.o …
The r
option creates an archive of the provided library object files, c
option initiates the archiving process and s
option creates the archive’s index (symbol table).
The created static library file (mylibrary.a) is now portable and ready for use.
How to Use Static Library in Linux
To demonstrate the usage of this static library, let us create a driver program with the help of a C file.
$ nano driver.c Or $ vi driver.c
Add the main function using the following code:
/* filename: driver.c */ #include "mylibrary.h" void main() { task(); }
Create the library object from this C file:
$ gcc -c driver.c -o driver.o
We will now use the generated library object to create a driver program and link it to our static library.
$ gcc -o driver driver.o -L. -l:mylibrary.a
The -L.
point to the location of the static library i.e current folder and -l:mylibrary.a
creates a static link between the static library and the driver program.
We can now test the execution of this driver program:
$ ./driver
The output “task() called from a static library” originating from the C file we created earlier confirms that the driver program and the static library are statically linked.
Modifying Static C Libraries
In reference to the above-covered steps that led to the creation of a static library (mylibrary.a), we can conclude that the creation of a static library only requires the existence of valid object files (library objects). Therefore, we can redefine a static library as a composition of library objects.
Viewing the Content of a Static Library
Suppose, we want to display all the library objects that make up our static library, we will use the ar command:
$ ar t mylibrary.a or $ ar tv mylibrary.a
The -t
command option lists all the object files present and -v
is for verbose display and provides a detailed preview that includes file attributes like owner, group, timestamp, and permission.
Extracting Library Object from Static Library
If you need to extract and inspect a library object from a static library, utilize the ar command option x
. We can also combine the command with the verbose option (v)
for the interactive execution of the command.
The following command extracts all available library objects.
$ ar xv mylibrary.a
The following command extracts a single specified library object.
$ ar xv mylibrary.a mylibrary.o
The following command extracts multiple specified library objects.
$ ar xv mylibrary.a lib1.o lib2.o lib3.o
Adding Library Object to Static Library
Supposing the static library mylibrary.a needs to accommodate additional library objects, we will make use of the ar command option r
. We can even combine it with the verbose option.
This command adds a single specified library object to the static library
$ ar rv mylibrary.a mylibrary.o
This command adds multiple specified library objects to the static library.
$ ar rv mylibrary.a lib1.o lib2.o lib3.o
Afterward, view the content of the static library to confirm the addition of the selected library objects.
$ ar tv mylibrary.a
Deleting Library Objects from Static Library
If you want to delete single or multiple library objects from a static library, make use of the ar command option d
. Combine it with the verbose option to see the library object files being deleted.
This command deletes a single specified library object from the static library
$ ar dv mylibrary.a mylibrary.o
This command deletes multiple specified library objects from the static library.
$ ar dv mylibrary.a lib1.o lib2.o lib3.o
Conclude
A major advantage associated with static libraries is their ability to accelerate the program execution process. However, if the static library undergoes any changes, a user will have to every time recompile the main program for the changes to take effect.
That said, you can now employ the concept of static libraries in your current and future Linux projects.