You don’t need to reinvent the wheel …use libraries

¿Why using libraries?

……………………………………………………………………………….

A “library” is a collection of data and programming code that is used to develop software programs, that save the programmer from needing to “reinvent the wheel”. All of the available functions within a library can just be called/used within the program body without defining them explicitly.

Dynamic and static way

……………………………………………………………………………………

When a program is compiled, the compiler generates object code, a sequence of statements or instructions in a computer language, which can in turn be linked to form an executable file or library file. One of the main tasks for linker is to make code of library functions available to your program; this task can be done in two ways:

  • By copying the code of library function to your object code — Static way
  • By making the code of the library functions available at runtime, instead of being copied — Dynamic way

— — — — — — — — — — — — — — — — — — — — — —

Static libraries

…………………………………………………………………………………

According to what was seen previously, static libraries are object files that are combined with another object to form a final executable through the linking process. They have the prefix lib and the suffix .a — for example, libexemple.a

Some advantages

· In static libraries, once everything is included in your program, you don’t have to worry about the client having the library available on their system.

· Execution is fast because the object code is already included in the executable file.

Drawbacks

· Static libraries create a memory and space overhead every time they are used because they are part of the executable.

· Any change in static libraries involves compiling the program every time.

How to create static libraries

………………………………………………………………………………

The object files must be previously generated with gcc and the -c flag:

gcc -c betty_file.c

We will see how to create static libraries through ar program:

ar rc libholberton.a betty_file.o

This command creates a static library named ‘holberton.a’ and puts copies of the object file “betty_file.o” in it. If the library file already exists, it has the object files added to it, or replaced, if they are newer than those inside the library. The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells it to replace older object files in the library with the new object files.

After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation:

ranlib libholberton.a

How to use static libraries

……………………………………………………………………………………

gcc main.c -L. lholberton -o function

This will create a program using “main.c”, and any symbols it requires from the “holberton” static library. Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the ‘-L’ flag — this flag tells the linker that libraries might be found in the given directory (‘.’)

Dynamic libraries

…………………………………………………………………………………

Linker process happens when the program is run, when both, the binary file and the library are in memory. Dynamic ibraries have the same lib prefix as static libraries but the suffix is .so for indicate they are shared objects.

Some advantages

· Executable files are reduced in size, because much of the code will be stored in libraries.

· Dynamic library can be updated/modified without a need to re-compile our programs.

Drawbacks

· A change not compatible with previous versions of the dynamic library on which your code is based could prevent the program from running.

· If a copy of the required library cannot be guaranteed, the program will not be able to run.

How to create dynamic libraries?

…………………………………………………………………………………

1- Copy all the files that you want to be part of your dynamic library into the same repository

2- Create a header file including the prototypes of all the functions you want to include in your dynamic library.

3- After that, you can compile all the c and create the library using the following command:

gcc -Wall -pedantic -Werror -Wextra -fPIC *.c -shared -o liball.so

· The -Wall -Werror -Wextra -pedantic flags are used to enable compilation warnings

· The -fPIC flag allows the generation of “position independent code”, this allows the code to be located in any virtual address at runtime.

· The -shared flag created a shared library. The -o created an output file. When you use both, you created a shared output file name liball.so.

4- We can make the library available at runtime by prepending the current working directory to the LD_LIBRARY_PATH environmental variable:

Export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
  • The export command will allow you to set values for the LD_LIBRARY_PATH shell variable.
  • The .: following the equal sign specifies that you want to prepend the current working directory to the shell variable.

How to use dynamic libraries

……………………………………………………………………………………

When using the dynamic library, type the following command:

gcc -Wall -pedantic -Werror -Wextra -o program.c program liball.so

This will create a program using “program.c”, wich required the liball.so dynamic library.

— — — — — — — — — — — — — — — — — — — — — — — — — —

I hope you enjoyed the article, I recommend you watch this video 🤓

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store