Types of library

¿Why using libraries in general?

Takeshi Camilo Ramirez Escobar
4 min readMay 5, 2020

--

Library is simply a collection of functions which can be added to your application and the functions called as necessary, just like any other functions in the application. More precisely any object, not only functions can be stored in a library, but the vast majority of libraries only contain functions.

The actual implementation is language/system specific. Assuming that you’re building a C/C++ program on Windows, you have (at least) these options:

  • Static library. (lib) Compiled to object code by the compiler and the objects merged into a library by the linker/librarian tool. (usually coming with the compiler suite)
  • Dynamic linked library. (DLL) This is a windows specific thing. When loading the application, the operating system automatically links the application with the library. The linking operation is basically the same as the normal linking of executables, the difference is that the final executable is created when loading the application, not when building it.
  • DLL or other library file opened by the application and the functions called through function pointers. (AKA shared library concept on other systems) Typical use case is the plugin system for many applications. The plugins are basically libraries.

¿How do they work?

Most of the functions declared in header files are implemented in libraries. However, there’s no mechanical or automatic relationship between the functional prototypes in the header files and their implementation in a library. The .h file is maintained by hand, by the programmer, and is used to generate a library. The header file and associated library are distributed and installed together (one hopes), but correct installation and subsequent use by the compiler & linker require human beings to keep track of the pair. Failure to do so leads to “interesting” development and even run-time problems, especially with libraries whose functions’ parameters change from version to version

¿How to create them (Linux only) and How to use them (Linux only)?

Create Header file (static libraries)

Create libraries (Dinamic Libraries)

¿What are the differences between static and dynamic libraries?

In programming, a library is a collection of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on
which they can be reused in the programs.

Static Libraries : A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static.
They are usually faster than the shared libraries because a set of commonly used object files is put into a single library executable file. One can build multiple executables without the need to recompile the file. Because it is a single file to be built, use of link commands are simpler than shared library link commands, because you specify the name of the static library.

Shared Libraries :
Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files.
These are linked dynamically simply including the address of the library (whereas static linking is a waste of space). Dynamic linking links the libraries at the run-time. Thus, all the functions are in a special place in memory space, and every program can access them, without having multiple copies of them.

What are the advantages and drawbacks of each of them

When considering the advantages and disadvantages of Static and Dynamic libraries, we may want to consider size, speed, and updates. Since the actual code from Static libraries is written into your program, when it comes time to run the program, the code is already there, and so, your program may run slightly faster than programs that must, at run time, go and retrieve code at a given memory address. However, your object code files will be larger when using Static libraries, and if you have many of them, the added space taken up on the disk may be an issue to consider. Another issue with Static libraries concerns updates.

If they are few in number, this may not be an issue, but if there are many, Dynamic libraries may be preferable. So now, when we consider Dynamic libraries, we can see their advantages and disadvantages.

Bibliography

--

--