Everything you need to know about static and dynamic libraries!??
https://www.dhirubhai.net/pulse/everything-you-need-know-static-dynamic-libraries-ram%25C3%25ADrez-mojica/?trackingId=FCp6PtX2REeh4duUJfpYtw%3D%3D

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 available or that we create ourselves to be used in our different programs.?This gives us many advantages when building our code, because we have the facility to call a function in any part of it without having to write it always "from scratch". Let's look at an example:


* This is a file named myheader.h *
        

#ifndef MYHEADER_H

#define MYHEADER_H


int multiplication (int number1, int number2);

#endif        

Let's suppose that the above is a header file?.h?which contains a function prototype called?"multiplication", this file has the possibility of being imported into another file, mainly in a?.c?(or it can even be the same?.h) in which we can call the function?"multiplication"?that we have created as many times as we want and pass the parameters that are required without the need to be coding the content of our function.

* This is a file named myfunction.c *

* Here we code what we want our function to do, this is what we would always have to write in our code if we did not have the library. *

int multiplication(int number1, int number2)
{
  return (number1 * number2);
}        

Our function is now ready. To be able to call it in another file, we must include the library we created using the keyword?"#include"?in the first lines inside the file we want to use it.

* This is a file named myprogram.c *

#include "myheader.h"        

And when we get to the part of the code where we want to use our multiplication function without having to write the return and so on, we do it by calling the function and passing it the parameters we need as we have defined them in our header file earlier. Something like:


* myprogram.c *

#include "myheader.h"

...
...

multiplication(3, 5)

...        

Types of libraries???

There are two types of libraries,?static?and?dynamic.

- Static libraries:?In a way we can say that these are libraries that generate a copy of them in the program that we are using when we compile it. Once we have the executable of our program, practically the library as such becomes "useless", since we could eliminate it and our program would continue working, since, as mentioned before, a copy of its information was generated. Regarding this copy, it is important to know that only what is really required is copied, that is to say, if the library has two functions, but within our program we call only one of them, only that function is copied.

- Dynamic libraries:?Unlike static libraries, dynamic libraries do not generate a copy of the information in our program when we compile it. When we have our executable and we are running it, every time the code needs something from the library, it will look for it in the library. If we delete the library, it will generate an error that the program cannot find it.

How to create a library????

  • Static libraries:?To learn how you can create a static library I invite you to read my previous article in which I discussed this point in detail, I leave you the link:


  • Dynamic libraries:

1. HEADER FILE WITH FUNCTIONS PROTOTYPES??

We must create a header file?(.h)?that contains the prototypes of the functions that we want to include in our library, it would be something like this:

No hay texto alternativo para esta imagen

2. COLLECTION OF ".C" FILES??

Include in your directory the .c files with the functions you want to add to your library and you can list them to make sure they are all there without fail, for example:

No hay texto alternativo para esta imagen

3. COMPILE ALL ".C" FILES??

Now, we must compile all the .c files, this can be achieved with the gcc followed by the respective flags and the files with the special character "*" to avoid compiling them one by one, it would be something like this:

No hay texto alternativo para esta imagen

With the previous thing, we will generate?.o?object files that are the ones that we will compress to generate our library. Also, notice that the?-fPIC?flag was used, whose meaning is?"position-independent code", what it does is to generate the code for the dynamic library independently of where it is located, that is to say, as several programs can use something of the dynamic library, the library cannot store its data in a unique memory address, then, with this flag it is forced to create a different object file?".o"?for each source file?".c".

4. CREATING THE DYNAMIC LIBRARY??

We must compile using the?-shared?flag, which converts the object files into a dynamic library and thus makes all object files interlinked, followed by the?-o?flag which gives the output in a file to which we can give a name, the name of our library with the?".so"?extension, this means?"shared object", and finally the object files we want to include, we can use the special character?"*"?to accommodate all?.o.

The final command of the above would look like this:

No hay texto alternativo para esta imagen

The library is already created and ready to use.

Example of use:

Given the following header file, use the library function?"strlen", which returns the length of a string.

No hay texto alternativo para esta imagen

*Important, consider:

For this example, our test file will be named?"0-main.c", our library?"libdynamic.so"?and our final result will be saved in an executable file named?"len".

If for example I want to use the?"strlen"?function found in my library, I can do it in the following way?(step by step):

1. CREATE A TEST FILE IN WHICH THE FUNCTION IS CALLED??:

For example:

No hay texto alternativo para esta imagen

2. COMPILE THE TEST FILE WITH THE LIBRARY??:


No hay texto alternativo para esta imagen

In the previous command, the?"-L."?flag gives the order to the compiler to search for the library in a certain location, in this case it is in the current directory, that is why we write the?".", followed by our test file, the?-l?option that allows us to search for our library (the name of the library must be specified and it is not necessary to write the shared object extension?".so"?since the compiler assumes it by default) and finally we indicate that the final result (the?"strlen"?function already executed) will be saved in our?.exe?file?"len".

3. LIST SHARED LIBRARY DEPENDENCIES??:

For this, we must use the?"ldd"?command, the use of which is quite simple, we run the command?"ldd"?together with an executable file and you will see all the dependencies of the shared libraries in the output.

No hay texto alternativo para esta imagen

As we can see in the previous image, we get an error message?"not found", this is because the program needs to know where to look for the library files, so we have to go to?step #4.

4. ADD THE LOCATION OF YOUR LIBRARY FILES IN THE SPECIFIC ENVIRONMENT VARIABLE??:

For this, we must use the environment variable:?"LD_LIBRARY_PATH"

In this environment variable, there is a set of directories in which libraries should be searched first, before the default set of directories (Similar to Shell's?"PATH"), this is used when manipulating a new library or using a non-standard library for special purposes.

Its use is:

No hay texto alternativo para esta imagen

The?"."?indicates that the library files will be searched for in the current directory and as we can see, if we execute the command ldd,?we no longer get the previous?"not found"?error message because now the library files have an established location.

5. RUN OUR FINAL FILE??:

After all the previous steps, we have our executable file?"len", which will return the length of the string specified in our test file, remember that its name is?"0-main.c".

No hay texto alternativo para esta imagen

Advantages???and disadvantages??

  • A program compiled with?static libraries?is heavier, since all the information needed is copied.
  • A program compiled with?static libraries?can be moved to another computer without having to move the libraries as well.
  • A program compiled with?static libraries?is faster in execution, because when a library function is called, it is in your code and you do not need to read the file as in the case of the?dynamic library?to find the function and run it.
  • If we modify a?static library, the executable files will not be affected, something that happens in a?dynamic library. This can be positive in case we want to solve errors for?dynamic libraries, but negative if, for example, we add one more parameter to a function, this will generate an error.


No hay texto alternativo para esta imagen

Advantages???and disadvantages??

  • A program compiled with?static libraries?is heavier, since all the information needed is copied.
  • A program compiled with?static libraries?can be moved to another computer without having to move the libraries as well.
  • A program compiled with?static libraries?is faster in execution, because when a library function is called, it is in your code and you do not need to read the file as in the case of the?dynamic library?to find the function and run it.
  • If we modify a?static library, the executable files will not be affected, something that happens in a?dynamic library. This can be positive in case we want to solve errors for?dynamic libraries, but negative if, for example, we add one more parameter to a function, this will generate an error.

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

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…

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

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

社区洞察

其他会员也浏览了