Do you know anything about static libraries in C ?

Do you know anything about static libraries in C ?

In this opportunity we have as a main topic the static libraries in C

we are going to talk about topics such as:

  • Why use libraries.
  • How they work.
  • How to create them.
  • How to use them.

> First, what are the libraries in programming?

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( file.o). Libraries are particularly useful for storing frequently used routines because you do not need to explicitly link them to every program that uses them.

Well, now let's get into our main topic :

In C a, a static library is a compiled object file containing all things required by the main program to operate.

No alt text provided for this image

These are loaded near the end of the build during its linking phase.

1 => Why should we use them?

Without having much detail about what you are doing behind the scenes, there is a very big runtime advantage, because we are putting together all the symbols that the program is going to need for work.

2 => How they work and How to create them?

About how the library works internally we already talked about above but it can be seen implicitly during these cases

Steps to create a static library:

1- put all relevant files for your program in one directory next to main.h with all required prototypes.

No alt text provided for this image

2- Compile all ".c" files with the -c option to create a object file for each .c file

with a command like this => gcc -c *.c

No alt text provided for this image

3- with the following command => ar -rc library_name.c *.o to archive all .o files into a static library .a file

No alt text provided for this image

3 => How can we use them?

Then of followed this steps

Instead of having to include all relevant file names in the compilation command, only the library (.a) file need be referenced

with a command like this => gcc main.c library_name.a

As a conclusion or closing we can say that using a static library in our programs can be very helpful, reducing the programs size taking up less space on the disk.

This article was very useful to me for write this :


If you got here I hope this article has been of interest to you and thank you very much for reading me :)







Mauro Labella

STEM MBA Candidate at The George Washington University | Finance-Risk Management | System-Operations | Business | Taxes

3 年

Very interesting, thank you!

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

Santiago Neira的更多文章

  • Have you ever asked yourself what's going on behind your searches?

    Have you ever asked yourself what's going on behind your searches?

    Have you ever wondered what happens when you search for something in your browser? probably you answer will be no…

  • Have you heard about the IOT?

    Have you heard about the IOT?

    First question, what is the Internet of things ? in symply words is the concept of connecting any device that you can…

  • Recursion

    Recursion

    First, what is it? Recursion is a broad concept, complicated to think about, although we can see it in daily activities…

  • Mutables and Immutables Types..

    Mutables and Immutables Types..

    Python is an object-oriented programming language, in python, everything is an object. Let's see what this means: We…

  • Dynamic and static libraries in C

    Dynamic and static libraries in C

    First of all, what are bookstores? We must first ask ourselves what a programming library is, it is a type of file that…

    1 条评论
  • Behind the compilation in C Language

    Behind the compilation in C Language

    This time in the blog we will focus on learning a little more about the compilation process of this programming…

社区洞察

其他会员也浏览了