Dynamic libraries

Dynamic libraries

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

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, by being able to include these libraries in our executables we can save ourselves a great deal of work.

How do dynamic libraries work?

It is NOT copied into our program when we compile it. When we compile our program and are running it, every time the code needs something from the library, it will go to the library. If we delete the library, our program will give an error that it cannot be found.

How to create dynamic libraries?

1) To create a library of this type, we must first have all the .c files to use in the same directory, then execute this command that will create the .o (object code) files for them.

The -fPIC flag generates position-independent code for shared libraries.

- *.c wildcard which makes it apply to all .o files

No hay texto alternativo para esta imagen

2) Finally we will execute this command that will transform the object codes into a library with the name that we indicate after the -o

-shared is a flag that tells the linker to create a shared object.

- *.o wildcard which makes it apply to all .o files

No hay texto alternativo para esta imagen

How to use dynamic libraries:

To use the dynamic library that we created, you must add the location of your library in the environmental variable LD_LIBRARY_PATH so that the compiler can find the object code of the functions that they use, to do this, use this code

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

For example, if you want to use some functions of the created library in your code, you must instantiate it at compile time.

No hay texto alternativo para esta imagen

Differences between static and dynamic libraries

The differences between these two libraries is found when implementing their functions. In the static libraries the code of each function is introduced in the program used, in this way it is not dependent, on the other hand when using dynamic libraries the program calls the Library functions used at runtime.

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).


Valentin Repetto - 2022.

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

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…

  • 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”…

  • 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…

  • 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)…

社区洞察

其他会员也浏览了