Static and dynamic libraries

Julian Torres
3 min readSep 7, 2020

Why use libraries?

Free wills are files (not always external) that allow us to carry out different tasks without having to worry about how they are done but simply understand how to use them. The C-freeways allow us to make our programs more modular and reusable, also making it easier to create programs with quite complex functionalities in a few lines of code.

How to create static libraries and use them:

In order to create a static library using GCC, we need to compile our library code into an object file, we achieve this using the -c flag with GCC

$ gcc -c * .c

All .c files in the current directory have been converted to object files, now with the ar(keeps filegroups as one compressed) command we will create our library

$ ar -rc library.a * .o

Once our file is created, we must index it what we will do with the ranlib command. this command creates an index of the contents of the library and stores them.

$ ranlib library.a

Now that we have our library created we can list the objects of this as follows

$ ar -t library.a

Another way to list this would be with the nm command, which would list the files with the objects it contains.

$ nm library.a

Now using the following GCC indicators we can make use of our library

$ gcc main.c -L. -lrary -o main

  • -l<libraryname without lib prefix and extension>
  • -L : specifies the path to the library .We can use -L. inorder to point to the current directory and -L/home/tmp to point to the /home/tmp directory.

How to create dinamic libraries and use them:

$ gcc -g -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o libholberton.so

The -g flag include debugging information

The -fPIC flag enables “position independent code” generation, which is a requirement for shared libraries. This allows the code to be located at any virtual address at runtime.

The -shared flag created a shared library, hence the .so extension for shared object. The -o created an output file. When you use both, you created a shared output file name libholberton.so.

The -Wall -Wextra -pendantic flags are all warning and error flags that should be included when compiling your code

When we use this sequence of flags with the gcc command, we are compiling all the C files in the current directory and created a shared library called libholberton.so that now exists in memory on our machine.

Differences between Shared and Static Libraries?

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.

--

--