STATIC LIBRARIES IN C

STATIC LIBRARIES IN C

WHY USE LIBRARIES?

A library is a set of functions. It is like a program. It contains typical functions to be called from some other program or library to perform a common task and any developer can create a new library. For example, the gcc compiler automatically links the standard C library to its programs.

Static libraries are object files that are then combined with another object to form a final executable.

This way we can use them whenever we want. The huge advantages of this are:

  • You don't need to rewrite the code.
  • You save the time of compiling that code each time because it will already be compiled.
  • The compiled code will be tested and reliable. You may have to fix bugs the first few times and it may work correctly in different programs.
  • They are linked when compiling, they are "inside" the final executable. On Windows they have the extension .lib. On Linux they have the extension .a

HOW THEY WORK?

To use a library, we must have:

The headers are the files with .h extension that contain the ''declarations'' (also called ''prototypes'') of the functions (A function that is ''defined'' inside a header is called an ''inline'' function).?

They are invoked through the ''preprocessor directive'' (so called because it is done at the ''preprocessing'' stage of the compiler) #include, which performs a simple substitution. The compiler must be able to locate the headers.

HOW TO CREATE THEM?

To create a static library it is necessary to follow the steps below:

  1. In a directory contain all the .c files, including the .h file containing the prototypes.

No hay texto alternativo para esta imagen

2. To convert all .c files to objetc (.o) files at once, this script is applied at the prompt (with or without flags):

No hay texto alternativo para esta imagen

If you list the files they will look like this:

No hay texto alternativo para esta imagen

3. Now, there are all the object files to be added to your library. Then, to create a library and add those files, you must use this simple script in the directory where all the files are (.o).

No hay texto alternativo para esta imagen

You can apply this "ar -t" script to see what is inside your library.

No hay texto alternativo para esta imagen

4. Finally, as physical libraries you have to index or sort the information you have there.?ranlib generates an index of the contents of a file and stores it in the file. The index lists each symbol defined by a member of a file that is a relocatable object file. A file with such an index speeds up linking to the library and allows routines in the library to call each other regardless of their location in the file. This is a good practice because it will make the linking step faster.?

No hay texto alternativo para esta imagen


HOW TO USE THEM?

Now that a static library is created and it allows us to use it by invoking it as part of the linking process when creating an executable program. You can do this easily, just use the flag when you apply the gcc compilation code, like this:

This is the fuction main

  • -l: library name without prefix and lib extension.
  • -L: specifies the path to the library. You can use -L. to point to the current directory and -L /home/static to point to the /home/static directory.

No hay texto alternativo para esta imagen
No hay texto alternativo para esta imagen

There is an important detail to take into account. The libraries must be placed in such a way that the highest level library is first and the lowest level library is last.

Static linking is useful, but it has two main disadvantages:

  • If you update the library code, you must recompile your program.
  • Every program on the system that uses that library contains a copy in its executable. This is very inefficient and makes it worse if you encounter an error and have to recompile.

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

Liseth Arias的更多文章

社区洞察

其他会员也浏览了