Do Something Great
Let us Create and Use a Static Library in UNIX
![](https://miro.medium.com/v2/resize:fit:599/1*u-EIkxofeU-pmEwR1H9iWQ.jpeg)
Ken Thompson and Dennis Ritchie — UNIX Operating System
These Superhero innovators have made significant advances in our daily lives and well-being since 1971: while working at AT&T Bell Labs in the USA
Fathers of C Language, highly respected ; they both have inspired all the others languages
Today in this article, I will cover the Concept of Statical Libraries in C
The Power of Static libraries
Why use them
How they work
How to create your own
Since we need some pre-defined variables and functions for our code in our daily tasks x times in a day . It has to be automated.
Think of the time you’d save.
Libraries are collections of these header files you need ; that you ‘d use in your code to access the appropriate functionality . These functions and variables are divided into related groups called header file.
Remember , C is a compiled language , Is the compiler which turns source code into object modules not humans and then, the linker do the job , the linker combines object modules together to have an executable = the library file.
Some examples of header files:
- <stdio.h> It contains functions like printf(); scanf();
- <string.h> It contains functions like strlen(); strrev();
- <math.h> It contains functions like rand(); pow();
note : library file is the actual executable code that does the work as specified in that header file.
The loader re-links every time you run because is not stored in memory.
They are large Benefice of using Static library in C :
- Reduce compile time
- Only the linker to do the job
- Binary are easier to manage , handle errors during execution. They also simplify design and maintenance .To sum up , a static library is basically a set of object files that were copied into one single file with the suffix
.a
This is a very useful tool and a perfect time-saver for programmers
Create a static library , also called archive
with the GNU compiler version (GCC 9.4)
Check the GCC version or uname to determine the processor architecture, the system hostname and the version of Kernel running on the system to check in wich context your are actually working; it will help you
gcc -v
The syntax of the uname
command takes the following form:
uname [OPTIONS]...
Now, for this mini-tutorial that drills this whole concept:
We will need three files
1. Create a C file that contains functions
![](https://miro.medium.com/v2/resize:fit:700/1*nOzVhe8j-3hlXifZVRUErw.png)
2. Create a header file with all your prototypes functions
![](https://miro.medium.com/v2/resize:fit:700/1*QnnAytQbgzVrXhn6j1wKpA.png)
3. Compile library files.
- They need to be linked to be executable. You can use this command below:
gcc -c file.c -o file.ogcc -o file file.o -L. -l_mylib
We have .o files that we want to merge into a single file (library file.a)
-Flags
-Wall -Werror -Wextra gnu89
According to GNU Documentation Warning Options (Using the GNU Compiler Collection (GCC))
I will say only that -Wall converts all warning to error to keep it simple
Testing
3'. Create a main.c file calling your .h file and include standard library in the header for testing.
![](https://miro.medium.com/v2/resize:fit:700/1*kMcrwxqFCiJntDnzIkWLpQ.png)
![](https://miro.medium.com/v2/resize:fit:623/1*IpABuAM3FODw-i-YvkQxFQ.png)
Archiver
Use the ar command with the flag -rc, followed by the name of your library and your file.o one by one or using *.o ( to select *
all files with extension .o)
![](https://miro.medium.com/v2/resize:fit:700/1*_YBIE9brRNYYg9oEvZ0OiA.png)
Index
- now index your static library with the ranlib command.
ranlib is optional: it generates an index of symbols in the archive and puts it in the archive — this helps speed up the process of linking.
![](https://miro.medium.com/v2/resize:fit:700/1*9osJvEbxR0ESGK11GM9pnA.png)
For large libraries, this becomes essential more than an option.😎
Finish
Terminology : .a files in Linux is the name of a static library generally divided into three parts
- Prefix :
lib
- Library name : <name.a >
- suffix :
.a
Now libname.a was created
The name of the final static library should be :<libname.a >
Even, if they are some errors from the viewpoint of a programming language, they are may be software bugs and key is testing
Header guards are conditional compilation directives that take the following form:
example of a main.h file
#ifndef _EXAMPLE_H
#define _EXAMPLE_H/* your declarations
*
*/#endif
Still Cannot solve the bug ?
Remember to provide the -L option specifying where a library is to be found before the -l option indicating the library itself.
Options passed on the command line for linking a library or executable don’t dynamically carry over to linking the next one.
(absolutly , we have to re-link the code again)
ld returned 1 exit status
In order to link to an archive library , do not forget to add it to the link line: check the -lname
I hope this article would be useful for you 😊
never give up #CisFun
Feel free to leave me a comment or questions
See you