Static Libraries in C .

Static Libraries in C .

Static 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 compiler , linker producing an object file and an executable.

Libraries are particularly useful for storing frequently used functions because you do not need to explicitly link them to every program that uses them. The linker automatically looks in libraries for routines that it does not find elsewhere. 

So How to create and use a Static Library?

In order to create the library first thing we need to have all our functions in separate C files (files with extension .c)

  • After we are done writing, composing ,coding our C files we will have to compile them to object files (files with extension .o or .obj) with the following command :

$ gcc -c funtion_name.c        

Here in this example we use the flag “-c” is to stop the compiler in producing the .o files in the end we give the .c file we want to compile

  • To compile all the .c files we created and compile them with all error checks we should use the following command line :

$ gcc -Wall -Werror -Wextra -pedantic -c *.c        

  • At this step we have created our .o files and we are ready to go create our library and add them, for it we use the next command :

$ ar -rc liball.a *.o        

The command "ar" is used to create archive file (.a)

The flag “-r” is to replace-overwrite .o files (just in case that library exists already)

The flag “-c”to create the library if it doesn’t exist or appends to it if it does

The “liball.a” in this example is how we want to name the library but always with the lib- prefix and ".a"extension.

The flag “*.o” to add all object files (we can choose one or more by adding their names).

  • The next step will be to index each file .o extension with the following command.

$ranlib library.a        

you can list you library to se the content with the following command :

$ar -t libholberton.a        

In this example it would look like this !

No hay texto alternativo para esta imagen

That is an example from my protect , it was my first library .

Cool at this point if we follow all the steps , we get our first static library!!

Now that we created it , lets see how to used it :

  • We going to compile our main.c file (the code for the application we want to create) and declare the library to our compiler.

$ gcc main.c -L. -lall -o program_name        

Use the command “gcc” to call the compiler.

The flag “-L.” is to tell the compiler to look for Libraries and the path to look at.

The flag “-lall” with this flag we are declaring the library’s name that we want to link (note that our library’s name is liball.a but we typed -lall, the linker needs only to know the custom name we give it and it will add automatically the preffix and extension)

The flag “-o program_name” is to defines the executable’s output name , you can run the command with out this flag and get the default output name “a.out".


I hope this information will be helpful to you , let's keep learning .

To view or add a comment, sign in

More articles by Laura Callejas

  • What does STEM mean?

    STEM stands for science, technology, engineering, and mathematics, and it has become the foundation of both education…

  • Web stack incident report

    In this blog, we are going to talk about software system failure, we are going to check the possible reasons for it…

  • What happens when you type google.com in your browser and press Enter?

    In the following article we are going to answer What happens when you type google.com and press enter? so we are going…

  • IoT (The Internet of things)

    WHAT IS IOT ? Well the Internet of things is a system of connected devices , computers and multiples digital machines…

  • What is recursion ?

    Is the process or concept in programming where the function calls itself, we use recursion cause for certain problems…

  • Everything is an object in Python!

    Python is an object-oriented programming language, and in Python everything is an object, an object is an entity that…

  • Static libraries vs Dynamic libraries.

    Libraries are a collection of files that contains functions in this case with extension .c for example stdlib.

  • What is GCC ?

    It is a compiler for GNU considered standard for operating system system derived from unix.The acronym GCC stands for…

Insights from the community

Others also viewed

Explore topics