Static and dynamic libraries.
By Vanessa Garcia

Static and dynamic libraries.

Libraries have the purpose of storing our own functions and that we use frequently, as for example, when a video game is developed, the code part is repeated over and over again to move an image around the screen.

Some of the advantages of libraries are:

  • Don't rewrite the code.
  • Save time in compilation, since this part of the code is already compiled. (The compiled code must be tested and must be reliable, and as far as possible it should have already been used in several programs so that possible errors are have corrected).

How to organize the code to create a library?

we need one or more source files with the extension ".c" that must contain the code of our functions.

At least one or more header files with an “.h” extension that must contain all the prototypes of our functions, struct, typedef, and other information necessary for our program.

It is important to bear in mind that we must write our prototypes inside the #ifndef - #endif block and we must also make a #define with the name of the library in capital letters preceded by "_" and ending with "_H_", for example #define_LIBRERIA_H_ This will avoid future conflicts when including the same header several times in the same program.

What is a static library?

At the moment of compilation the program makes a copy of the functions it needs, when we obtain the executable we could take it to another computer without problem, and we no longer need the library.

In Unix static libraries are usually called libname.a

Characteristics of a static library:

  • Makes the program executable bigger as it has a copy of all the functions you need.
  • It can be transported to another computer without problem since it has all the information it needs to run.
  • If any modifications are made to the libraries such as updates, bug fixes, etc., this will not affect the executable. which makes its maintenance more difficult.
  • In theory, a program compiled with static libraries is faster since it has all the information in your code and you don't have to go looking for it elsewhere in memory to execute it.

What is a dynamic library?

At the moment of compilation, the program does NOT make a copy of the library, therefore every time the program is executed and it needs something from the library, it will look for it. This means that if it does not find the library it will throw an error.

In Unix dynamic libraries are usually called libname.so.

Characteristics of a dynamic library:

  • The program will be much lighter by saving only the memory location of the library.
  • If you want to run the program on another computer, it must have the necessary libraries for the program to run correctly.
  • The program is likely to be slower as it must search for the library and then the information it needs to run.
  • When modifying the libraries, the executables will be affected, which facilitates the correction of errors in all the executables, but if for some reason a parameter is added to a library function, the executables will stop working.

How to compile and link the libraries

For this occasion we will focus on dynamic libraries.

1) First we must obtain the object files (". o") of the functions from the source files (".c") with the following command: (gcc for c)

Option 1:

gcc -c file1.c file2.c file3.c (...) filen.c -fPIC

Option 2:

gcc * .c -c -fPIC

The flag -c: Generates the object file (“.o”).

The flag -fPIC: Ensures that the code is independent of position, so it doesn't matter where the computer loads the code into memory.

2) After generating the object files, we must compile them into a dynamic library with the following command:

Option 1:

gcc -c file1.o file2.o file3.o (...) filen.o -shared -o libname.so

Option 2:

gcc * .o -shared -o libname.so

The flag -shared: compile all files (".o") into a dynamic library.

the flag -o: indicates that the information should be saved in the file named below (libname.so).

name: is the name assigned to the library.

3) Finally we must export the PATH of the library so that the programs can know where the library is, with the following command:

export LD_LIBRARY_PATH = $ PWD: $ LD_LIBRARY_PATH


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

Vanessa Garcia Vargas的更多文章

社区洞察

其他会员也浏览了