C Static libraries, How they work; Why and how we use them and how to create one

C Static libraries, How they work; Why and how we use them and how to create one


1. What is a Static library

A static library which is a statically linked library is a set of routines, external functions and variables that are resolved in a caller called compile-time and copied into a target application by a compiler.

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.

2. How to create a static library

No hay texto alternativo para esta imagen

Static libraries can be easily created. First we need to create the object files using the command


gcc -c *.c

This command will stop the compiling process before the linking phase and will create object files out of our .c files.


Next we will be using the "ar" command which is a program creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files (called members of the archive).


ar -rcs libname.a .*o

This commands will create a library called "libname.a" out of all our object files.


3. How to use static libraries


After creating our static library to use it we need to include it in our program.And when compiling program files, we have to tell the compiler to use the library files and where to find them. ‘-l’ tells it we want to include library files. And ‘all’ tells it to look for the library liball.a. It’s important to leave the ‘lib’ and ‘.a’ out of the flag because the compiler already identifies library files that way. ‘-L.’ tells the compiler it can find the library file in the current directory.


gcc main.o -L. -llibholbertonschool -o prog


4. How static libraries work

No hay texto alternativo para esta imagen

The way static libraries work is by supplying the executed program with an indexed list of all symbols such as variables, functions.. that are required by the program during the linking phase of compiling. They won't be required during run-time because all their components are imported into the executable file.

要查看或添加评论,请登录

David Stiven Perlaza Valencia的更多文章

  • Web infrastructure design

    Web infrastructure design

    Definition: Web Applications Infrastructure/Web Infrastructure also called internet infrastructure is the physical…

  • Everything is a object...

    Everything is a object...

    In this blog I will talk about Object Oriented Programming and Objects in Python. Really, everything is objects.

  • How object and class attributes work.

    How object and class attributes work.

    A class is a custom data type, In python, we can create custom classes that are fully integrated and that can be used…

  • Everything you need to know about static and dynamic libraries!??

    Everything you need to know about static and dynamic libraries!??

    do we call a library in the C language? ?? Basically, we can say that C libraries are a set of functions that we have…

  • How the Compilation Process Works for C Programs

    How the Compilation Process Works for C Programs

    In order to explain all the steps of compilation, we need to clarify a few programming concepts beforehand. In this…

社区洞察

其他会员也浏览了