Static Libraries in C Language

Static Libraries in C Language

No hay texto alternativo para esta imagen

Why we use them?

In C programming, static libraries are used to solve several points of the compilation; One of them is to optimize the code in our .c files, and the most common is to optimize the compilation time in large projects.

How they works?

No hay texto alternativo para esta imagen

basically it works like a whole group of tools created in order to reduce and optimize the compilation time so that the processes do not have to call standard rambling libraries, but compile only one in a single package, as the previous image shows. In this image we see how a static library is created, based on .c files and .o files.

How to create them?

to create a static library you need two types of files, some .c and others .o, on UNIX systems, we can compile the .c files to generate the .o files that we need to create the static library, for this we can use the GCC compiler with the help of arguments and flags, such as:

No hay texto alternativo para esta imagen

This command will create the .o files from the .c files that are in this directory, and with these .o files, we can create our library with the following command:

No hay texto alternativo para esta imagen

ar -rc nameoflibrarie.a *.o => The syntax is: the command and its flags, then the name that we will give it finished in an "extension" .a and finally the files we need, in this case the * will bring us all the .o files. With this command we can generate our static library for our project.

How to use them?

Finally we can use it in our project, for this it is simply to add it when compiling our main file and that's it. For this we use the following command:

but first you have to index the library so that the compiler can add it to the moment of linking all the files, and this is done with the command: "ranlib mylib.a"

No hay texto alternativo para esta imagen

Thank you for reading :)

No hay texto alternativo para esta imagen


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

Jonatan Mazo的更多文章

  • Trading simulator

    Trading simulator

    Our goal is to create, develop and maintain an application where people can develop as traders in the world of…

  • What happens when you type some URL in your browser and press Enter?

    What happens when you type some URL in your browser and press Enter?

    This question is very common in job interviews for software engineering candidates, many do not know the answer, others…

  • IoT, yes!, what you just read, Internet of Things.

    IoT, yes!, what you just read, Internet of Things.

    In this blog, you will find the basics you need to know about IoT (Internet of things), how it works, what technology…

  • What is Machine Learning?

    What is Machine Learning?

    For many it is a topic that they are little interested in or do not want to strive to understand what it is about…

  • Static and Dynamic Libraries

    Static and Dynamic Libraries

    In the C language two types of libraries are used in general cases, the first type is "Static Library", and the second…

  • The importance of a compiler in programming

    The importance of a compiler in programming

    Basically a compiler is software that transforms our codes, our ideas, our language, into a series of characters that…

  • Hard Link vs Soft Link in Linux

    Hard Link vs Soft Link in Linux

    A link in LINUX is a pointer to a file. Like pointers in any programming languages, links in LINUX are pointers…

  • What happens when you type ls *.c and hit Enter in the shell of Linux?

    What happens when you type ls *.c and hit Enter in the shell of Linux?

    To be honest, this command is quite simple compared to other Linux shell commands, but it is useful when looking for a…

社区洞察