Dynamic libraries in C

Img 1: Static and dynamic libraries in C

So to start, we need to know what a library is. A library is a set of functional implementations. is a collection of items that you can call from your program? A library is precisely like an executable, and the library’s functions are invoked with or without parameters from its executable.

Unlike an executable program, the behavior that a library implements does not expect to be used autonomously, but rather its purpose is to be used by other programs, independently and simultaneously.

Why we need to use libraries?

Libraries allow us to store function definitions that will be use in our executable program, as we said in the description before. Pprovide the user the benefit to use a variate of files that can be reused in different programs. Libraries are made of object files, with the extension ‘.o’, and can be made on Linux with the gcc compiler.

How libraries work?

Every library consists of two parts: the first parts is the header file that ends in .h contains all the information about the library that we are using, functions names, modules, directives, etc. The second and not less part is and the actual code file. A library is just like an executable, and the library functions are invoked with or without parameters from your executable.

img2 : How libraries work

Static libraries:

The static libraries are just collections of object files that are linked to the program during the linkage phase of compilation, and are not relevant at runtime, while the dynamics link when executing. If you want to know more about it, I will let you the article I wrote days ago about static libraries and how to create them STATIC LIBRARIES IN C

Anyways, we are going to give a little step by step of how to create a static library, for more details go to the link I left before

  1. Convert all source code you want to save as a library, into object files:
gcc -c *.c

2. Use the [ar] “archiver” program and call all the object file “.o”.

ar -rc libholberton.a *.o

3. Finally compile:

gcc main.c -L. -lholberton -o main

they have the suffix .so. they can be linked to a program at run time. They are loaded at runtime and are NOT included in the executable, which reduces the size of the executable files
They exist as separate files from the executable file. When you compile a program that uses a dynamic library, the library is not part of your executable.

So, how to create a dynamic library?

1. To start we should create the object files first with command:

gcc -fPIC -c *.c

“pic” stand for position independent code

Img 3: Writing gcc -fPIC -c *.c

So as you can see in the img we have created the object files with the –fPIC flag, that is a characteristic required by dynamic libraries.

2. Now we are going to create the dynamic libraries, and to so this we need to use the flag –shared. This flags tells the compiler to produce a shared object which can then be linked with other objects to form an executable. We call all the objects that ends with. o ¿, and by convention a library should start with “-lib” and end with “.so” (extension shred object) for the case of dynamic libraries.

gcc *.o -shared -o libholberton.so

now we have created our dynamic library:

Img 4: writing gcc *.o -shared -o libholberton.so

3. Now we can compile our programs using our dynamic library libholberton.so, running the next line command:

gcc 0-main.c -L. -lholberton -o len

our main.c file contains the next lines of code:

Img 5: main function

And we would get:

Img 6: compiling and creating our dynamic library

Useful commands to work with libraries:

  • nm: we used nm command when we need to find out what functions a library has, as we can see in the image
Img 7: writind nm command
  • LD_LIBRARY_PATH and ldd: LD_LIBRARY_PATH. It’s an environment variable, and contains a set of directories where libraries should be searched for first, before the standard set of directories. It is usefull in this case for Configure the loader where to find the shared library. and ldd is a command we used to list the dependencies of a library. The env variable and the command has to be use like you can see in the next image:
Img 8: writing env varaible and ldd command.

To finish lets gonna show some differences between static and dynamic libraries:

Img 9: graphical diferences between static and dynamic libraries

Static Libraries:

· They work from within the executable.

· Depends on the compilation to be modified.

· Make a static linking at compile time.

· Are faster. As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there.

·Increase the overall size of the binary, but it means that you don’t need to carry along a copy of the library that is being used.

Dynamic Libraries:

· Reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small.

· Work from outside the executable.

· We can use dynamic libraries in several programs

· It doesn’t depend on the compilation to be modified.

· Make a Dynamic linking of shared at run time.

· no need to recompile the executable.

Thanks for reading!