Libraries and how to use them

Andres Felipe Vasquez Chica
3 min readMar 1, 2021

A library is a set of prefabricated resources (algorithms) that can be used by the programmer to carry out certain operations. The declarations of the functions used in these libraries, together with some predefined macros and constants that facilitate their use, are grouped in files with well-known names that are usually found in predefined places. For example, on UNIX systems, in /usr/include. These files are usually called “header” files, because it is traditional to use the first lines of the program to put the #include directives that will include them in the source during the preprocessing phase.

Why should we use them

  • you are free to use the libraries you want in the structure you want.
  • You must control the compatibility of each library with the others.
  • You can use several libraries according to your needs.

How they works.

Static libraries

Historically, libraries could only be static. A static library, also known as an archive, consists of a set of routines that are copied into an application by the compiler or linker, producing files with object code and a separate executable file. This process and the executable file, is known as a static build of the target application. The actual address, references for jumps, and other routine calls are stored in a relative or symbolic address, which cannot be resolved until all code and libraries are mapped to final static addresses.

The linker resolves all unresolved addresses by converting them to fixed or relocatable addresses (from a common base) by loading all code and libraries into memory locations at run time. This linking process can take even longer than the compilation process, and must be done every time one of the modules is recompiled. Most compiled languages ​​have a standard library (for example, the C standard library), but programmers can also create their own custom libraries. Commercial compilers provide both standard and custom libraries.

A linker can work on specific types of object files, and therefore requires specific (compatible) types of libraries. Object files collected in a library can be easily distributed and used. A client, be it a program or a subroutine library, accesses an object library by referencing only its name. The linking process resolves the references by searching the libraries of the given order. Generally, it is not considered an error if a name can be found multiple times in a certain set of libraries.

Dynamic Libraries

Dynamic linking means that the subroutines of a library are loaded into a program at run time, rather than being linked at compile time, and are kept as separate files separate from the executable file of the main program. The linker does a minimal amount of work at compile time, records which library routines the program needs, and the index of names or numbers of the routines in the library. Most of the binding work is done at the time the application loads (load time or load time) or during execution (run time or runtime). The necessary linked code, called by the loader, is in fact part of the underlying operating system. At the right time the loader locates the libraries on the disk and adds the relevant data of these in the memory space of the process.

--

--