Daniel Ortega Chaux

May 3, 2021

3 min read

C Dynamic libraries

What is a library?

Differences between Static and Dynamic libraries:

Main differences between Static and Dynamic libraries
  • Is a compiled file which contains the symbols that the main program needs to operate, such as functions and variables.
  • When we call a function in the main, the compiler will link that code wrote in main with the code of the function that is in the library, and it will be added.
  • The process of compilation is still the same of the static libraries until it gets to the linker, at that point it will be added not the code of the function you called but the address where the function is located in the library.

How to create a Dynamic library in Unix? How it works?

  1. First, we need to compile and assembly our “.c” files. To do it we use the following line command:
  • -c-> Compiles and assemble, do not link.
  • -fPIC-> Flag Position Independent Code means that the generated machine code is not dependent on being located at a specific address in order to work.
  • *.c-> Compile all the .c files
  • gcc -c *.c-> will compile all the .c files and create a “.o” extension file which mean object code.
Convert .c files in .o files (object files)
  • -o->Is used to give a name to the executable file
  • My_file.o->The last file has to be the “.o”
  • This command creates a dynamic library called My_library.so and puts copies of the object files My_file.o in it.

How to use libraries?

  • $LD_LIBRARY_PATH->This environment variable is used to indicate in which directory it have to search for it.

How to use them: