Why to use libraries?

Oscar Morales
3 min readSep 7, 2020

--

Before read this story, I invite to you to read the following stories:

What are libraries?

Libraries are a collection of pre-compiled pieces of code that can be reused in a computer program. Libraries simplify life for programmers, library code is organized in such a way that it can be used by many programs that have no connection to each other, therefore, they provide reusable functions, routines, data structures and so on

Static libraries vs Dynamic libraries

What are Static libraries?

A Stactic library is a collection of pre-compiled pieces of code which are linked, with another pieces of code which are the program core (generally object files), by a compiler into a standalone executable.

The main advantage is that is more fast to read because all the code is put into the same executable. On the other, this causes an increase the size of the executable according to the libraries and object files that we linked. Also, we have to indexed new object files to the libraries and recompile the code if we want to make some changes

What are Dynamic libraries?

A Dynamic library is similar to Static libraries (collection of pre-compiled pieces…), but instead of being linked, it exist as separate file outside the standalone executable (Only is added the address of the library) and we use it, it is loaded into the memory directly

Dynamic libraries are much smaller than Static libraries because only one copy of the library is needed that is loaded and kept in memory. Also, Dynamic libraries no need to recompile the executable. The main disadvantage is that Dynamic library is susceptible to break because programs are dependent on having compatible library, that means that program will not run if we remove the library from the system or if the library becomes corrupt.

How to create and use Static libraries on Linux

I invite to you to read the following story where I explain all necessary steps to create and use a Static library:

How to create Dynamic libraries on Linux

  1. Create the object files for the library : gcc -fPIC -c object_file.c

The flag -fIPC generates a position-independent code avoiding any limit on the size of the global offset table, this is necessary because the library cannot store data at fixed addresses because many programs use the library

2. Create the dynamic library: gcc *.o -shared -o name_of_library.so

Instead of Static library, Dynamic library has a .so extension. -sharedis linker option that produces a shared object which can then be linked with other objects to form an executable

3. Export the path to the library:

export LD_LIBRARY_PATH=Path_of_the_library:$LD_LIBRARY_PATH

LD_LIBRARY_PATH`is the search path environmental variable for the linux shared library

4. Use the library:

gcc name_of_program.c -o name_of_executable name_of_library.so

Thanks a lot for read this story!

--

--

Oscar Morales
Oscar Morales

Written by Oscar Morales

Chemical engineer. Software developer student.

No responses yet