Static libraries in c

Static libraries in c

This time I will inform you about the static libraries in c and how they work

Whats are libreries?

Libraries are a group of files that have functionalities defined by third parties, and that can be used by any executable. Libraries contain variables and functions inside, certain types of files that we can import or include in our program are known as libraries (or libraries). These files contain the specifications of different functionalities already built and usable, such as reading from the keyboard or displaying something on the screen, among many others.

Whats are static libraries?

It is a library that is "copied" into our program when we compile it. Once we have the executable of our program, we could delete it and our program would continue to work, since it has a copy of everything it needs. Only that part of the library that is needed is copied. For example, if the library has two functions and our program calls only one, only that function is copied.

What are the advantages and disadvantages of each of these types of bookstores?

  • A program compiled with static libraries is larger, since everything it needs is copied.
  • A program compiled with static libraries can be taken to another computer without the need to take the libraries.
  • A program compiled with static libraries is, in principle, faster in execution. When you call a library function, you have it in your code and you don't have to go read the dynamic library file to find the function and execute it.
  • If we change a static library, the executables are not affected. If we change a dynamic, the executables are affected. This is an advantage if we have changed the library to correct a bug (it is automatically corrected in all executables), but it is a drawback if touching that makes us change the executables (for example, we have added one more parameter to a library function , ready-made executables stop working).

How to create a static libraries?

1)

  • First you have to have all the .c files that contain the functions and also the header file with the prototypes.
  • We will have to add at the top of our file with prototypes: ifndef and define.
  • then at the end :endif.

No hay texto alternativo para esta imagen

We use this so that the header file is only defined once instead of every time it's called, if you don't use it you might have some problems when compiling.

No hay texto alternativo para esta imagen

2) The next step is to create the .o files, we will do this using the gcc compiler with the -c parameter and the *.c wildcard.

  • gcc -c *.c

No hay texto alternativo para esta imagen

3) To create the library file, which is actually an archive file, we'll use ar.

We are using the -c (create) option to create the library file, the -r (add with replace) option to add them to the library file, and the *.o wildcard to add all the files we have to them.

  • ar -rc mylib *.o

4) Time to use our new library

At compile time we won't need to include all the relevant filenames in the build command, we just need to reference the library file mylib.a

  • gcc main.c mylib.a

I hope this information has been of use to you, thanks for reading it.

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

Valentin Repetto的更多文章

  • Bun 1.0

    Bun 1.0

    La reciente llegada de Bun 1.0 ha causado revuelo en la comunidad de desarrolladores JavaScript.

  • Nest js

    Nest js

    Introduction I have been hearing about nest js for a long time now, about its potential and features but I had never…

  • TypeScript

    TypeScript

    What is Typescript? Typescript is a programming language that is transpiled into Javascript. It is a superset of…

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

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

    Do you know what happens when you search for google.com in the browser? Internet is currently available to everyone…

  • Recursion

    Recursion

    The process in which a function calls itself directly or indirectly is called recursion and the corresponding function…

  • Everything is an object in python

    Everything is an object in python

    Introduction Objects can be defined simply as the instance of a class that contains both data members and method…

  • Dynamic libraries

    Dynamic libraries

    To start talking about dynamic libraries, we will remember what libraries are and what they are used for. Certain types…

  • What happens when you type `ls -l *.c` in the shell?

    What happens when you type `ls -l *.c` in the shell?

    INTRODUCTION In this occasion, and as part of the “simple_shell” project, we have to write about the “ls -l *.c”…

  • COMPILATION PROCESS

    COMPILATION PROCESS

    I will give a brief summary of how the compilation process works, from a high level language (that we can understand)…

社区洞察

其他会员也浏览了