Differences between static and dynamic libraries.

Jhon holberton
Analytics Vidhya
Published in
4 min readSep 8, 2020

--

Why using libraries?

Typically C functions/C++ classes and methods which can be shared by more than one application are broken out of the application’s source code, compiled, and bundled into a library. The C standard libraries and C++ STL are examples of shared components that can be linked with your code. The benefit is that every object file need not be stated when linking because the developer can reference the library collective. This simplifies the multiple-use and sharing of software components between applications. It also allows application vendors a way to simply release an API to interface with an application.

How do they work?

Dynamic library:

Dynamic libraries include the address of the beginning of the function that we are trying to use in the executable file.

Static library:

During the linking phase, the linker includes in the executable the modules corresponding to the functions and library classes that have been used in the application. As a result, such modules become part of the executable, just like any other function or class that would have been written in the body of the application.

How to create them? (Linux)

Static library

Suppose we have 2 source files and one header: exa1.c, exa2.c, exa.h. Now what we must do is use a command that its definition tells us: “create, modify and extract files”. This command is “ar” and together with its -rc flags, we can create our library:

ar -rc libexa.a exa1.o exa2.o

Now we must take into account 3 important things and they are:
1. The -r flag is used to replace or overwrite files that are repeated (that have already been invoked).
2. the -c flag is to create the library if it does not exist.
3. To obtain our object-files we must compile them in this way: gcc -c exa1.c exa2.c

Finally, to finish creating our library, all we have to do is index our library, which will do is list each symbol defined by a member of a file that is a relocatable object file:

ranlib libexa.a

Dynamic library

To create a dynamic library you must have 2 important things:

  • a header with the name of the prototypes of the functions of the “C” files.
  • We must have the “.o” files.
  1. First, we must compile our “c” files, using the command:

gcc -fPIC -c *.c

With gcc we will compile our files and with the flag “-c” we compile the source files “c” and with the flag -fPIC it generates “code independent of the position” and with “* .c” we are telling it to take all files ending in “.c”. This will generate our “.o” files

2. Now we need to use the command:

gcc -shared -o libnamedynamiclibrary.so *.o

With this command, we will create our dynamic library. using the -share flag to tell the compiler to produce a shared file that can be linked with other objects to form an executable and the -o flag with which we will indicate the name of our library, and finally, we use “* .o” to indicate that it takes all our files ending in “.o”

How to use them?

Static library:

Now to be able to use our library we must compile it in such a way that the program can understand where it will find the function of our files:

gcc main.c -L. -lexa -o exe

Considering this command:
- The -L flag will make it search for the library files in the directory.
- He “ . ” represents the current library.
- the -l flag helps us “link” the library. Note that the prefix “lib” and the extension “.a” are removed. In such a way that “lexe” would remain.
- With the -o flag we will be saying to name our executable file as “exe”.

Dynamic library:

For dynamic libraries, it is necessary to define the location so that the program knows where to look. we use:+

export LD_LIBRARY_PATH=. :$LD_LIBRARY_PATH

We have to define the environment variable LD_LIBRARY_PATH, in which we place all the directories where there are dynamic libraries of interest. now we verify that the library is already stored, using “ldd (library name)” and finally we can use “nm” to verify the symbols of the dynamic library with the “-D” flag to indicate that it is dynamic.

What are the differences between static and dynamic libraries?

Dynamic libraries:

These are dynamically linked by simply including the address of the library. Dynamic linking links the libraries at run time. Thus, all functions are in a special place in the memory space, and each program can access them, without having multiple copies of them.

Static libraries:

In static libraries, the libraries are added to the executable code so that the executable file can be loaded on any machine and run. Using the command file, we come to know that by default gcc follows dynamic binding and can be linked statically using the –static flag with gcc.

What are the advantages and drawbacks of each of them?

Static library:

+ faster.
+ without dependency.
- increased memory usage.

Dynamic library:

+ little memory usage.
+ If a .so has not been loaded yet, the executable’s remaining undefined symbols should be resolved at startup.
- There is the possibility of breaking executables without relinking.

--

--