Static and Dynamic Libraries, What is their difference?

Static and Dynamic Libraries, What is their difference?

All of us at some point have asked ourselves, what is a Programming library, and when we listen to Dynamic and static libraries, we tend to get confused because we do not understand very well what the difference is.

In this blog I am going to explain what a library is and what are the differences between Static Libraries and Dynamic Libraries.

The first thing would be to explain what are the libraries.

What is a Library?

Libraries are a set of methods or functions that help you speed up your work.

For example, a library could have a function to know the length of a string, or also concatenate it or pass it to an integer, there are many utilities that a library has that when coding help you a lot and save a lot of time.

No alt text provided for this image

Since I basically explain what libraries are, I'm going to talk about two types of libraries.

Static Library and Dynamic Library

Static Library

A static library is linked inside our executable, that means we can take it to another computer.

Dynamic Library

A dynamic library is not linked inside our executable, that means that our executable will be smaller.

No alt text provided for this image


Advantages and Disadvantages

well as in most things there is always an advantage and disadvantage.

I am going to show you what are the advantages and disadvantages of using any of these libraries.

Advantages

Static Library

  • We can pass it between computers without having to compile it in each one.
  • They are faster when they are executed because the functions are inside the executable and we don't have to be looking for them.

Dynamic Library

  • The executable is going to be smaller bone lighter.
  • If there is a bug in the library, it would only have to be updated and it will be fixed in the executables that use it.

Disadvantages

Static Library

  • If there is a bug in the library and a version appears that will fix that error, the entire code would have to be recompiled.
  • They are Bigger by leading to linked libraries.

Dynamic Library

  • If we take the executable to another machine (computer) the libraries should go with it.
  • Execution is slower because you have to go to find the libraries outside the executable.
  • If a function is changed (the parameters, the behavior) we would have to recompile everything.

What type of library do I use then?


How are they compiled?

Static

To get a static library, once we have our code ready, we would have to follow these steps:

  • To compile it is very simple, we have our files (.c) with our code (functions, projects), we create the object files (.o).
gcc -c file.c -o file.o

The -c option tells the compiler not to create an executable.

  • Create the library (.a). For this the ar command is used with the following parameters.
ar -rv libName.a *.o

The -r option tells the ar command that you have to insert (or replace if they are already inside) the object files in the library. The -v option is "verbose", to show information while doing things. and select all the object files.

  • Then we open the library we created and verify that all the functions are present.
nm libholberton.a 


Dynamic

To compile the same files, but as a dynamic library, we have to follow the following steps:

  • Compile the files as before to get the object files (.o)
gcc -c file.c -o file.o

or

gcc -fPIC -c *c

  • Create the library with the ld command.
ld -o libName.so *.o -shared

or

gcc -shared -o libName *.o

The -share option tells you to make a library and not an executable (default option).

  • Then we open the library we created and verify that all the functions are present.
nm -D libholberton.so



And that's it, this is how the static and dynamic libraries work, I hope I have been clear and that this article is to your liking, without saying more ... Until next time ...!


No alt text provided for this image

Follow me on Twitter:

@AdrianDeLaAsun1


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