Static Libraries \/ Dynamic Libraries

Rodolfo Delgado
5 min readMay 10, 2021

--

A library is a collection of resources, for example, functions, structures, values, and more.
Library code is organized in such a way that it can be used by multiple programs, for programmers is a easy way to reduce a lot of time reusing old code.

In this article I’m going to talk about static libraries and dynamic libraries.
Before that, it’s necessary know about the steps of compliation (if you want to know more, check this linked article)

In few words, the steps of complitations are:

  • Pre-processing
  • Compilation
  • Assembly
  • Linking

Static libraries and how create them

A static library is a file containing a collection of object files (ends with “.o”) that are linked into the program during the linking phase of compilation.
To learn more about the topic, I highly recomend you check static libraries!.

In a few words, static libraries are a collection of object files that are merged by the linker with another object file to form a final executable.
A static library in Linux ends with “.a”

Creating a static libraries…

Before creating a static library, is necessary have “.c” files like the next exmple.

“ls” command will display all the files in the directory

Here we have five “.c” files. In linux, we will use GCC to compile this files.

To change these files to object files, we need write the next command in the terminal…

Using “ls” again will show all the files and what gcc -c *.c do.

What these command mean?

  • gcc: it’s the compiler
  • -c: will compile and assemble, but do not link.
  • *.c : this flag will take only the files that ends in “.c”

When we have the object files, it’s time to create a static library to add these files, we have to use “ar(archive) with the next command…

Using “ls” again will show all the files and what ar -rc libname.a *.o do.

Using this command will create in the current directory a static library named “libname.a” and make copies of the object files in it.

What these flags means?

  • -rc: the “r” flag will add or replace existing object files in the library and the “c” flag will create a library if it doesn’t already exist.
  • *.o: this flag will take only the files that ends in “.o”

After this, if you are creating or modifying, it’s necessary index this archive. This index is later used by the compiler to speed up symbol-lookup inside the library.

An easy way to make it’s using the next command…

The command “ranlib” generates an index to the contents of an archive and stores it in the archive.

If you want to know more about how to use them check the next articule…

Dynamic libraries

Dynamic libraries are a collection of object files which are referenced at build time to give the executable information how they will eventually be used, but they aren’t used until run time, these objects are dynamically linked into executables that use them. The dynamic libraries ends with “.so”

How to Create a Dynamic Library

In the next example, we have three “.c” files…

“ls” command will display all the files in the directory

Like in static libraries, we need to use GCC to compile all the “.c” files but with different flags…

Using “ls” again will show all the files and what gcc *.c -c -fpic do.

What these command mean?

  • gcc: it’s the compiler
  • *.c: take all the “.c” files in the current directory
  • -c: the compiler will compile and assemble, but do not link.
  • -fpic: This flag ensures that the code is position-independent. This means it wouldn’t matter where the computer loads the code into memory.

As you can see, using the command “gcc *.c -c -fpic” we created object files. in the next step we will create the library named “hello”

Using “ls” again will show all the files and what gcc *.o -shared -o libhello.so do.

What these command mean?

  • gcc: it’s the compiler.
  • *.c: take all the “.o” files in the current directory.
  • -shared: Produce a shared object which can then be linked with other objects to form an executable.
  • -o libhello.so: Write output to file, in this case, to “libhello.so”.

Doing all the steps, we finally have the dynamic library!.

How to use a dynamic library.

To use it, first we need to export the library using this command

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

The program needs to know where to look for library files, we have add a path to the library to the LD_LIBRARY_PATH environment variable.

The dynamic library is ready for use.And now we can compile and run our program using our dynamic library with this command…

gcc mainfile.c library.so

Differences between static and dynamic libraries.

Static libraries

  • There are part of the build enviroment.
  • Object files are added to the executable file.

Dynamic Libraries

  • There are part of the run time enviroment (environment in which a program or application is executed).
  • Addres of the object files are added to the executable file.

Advantages and drawbacks of static and dynamic libraries.

Static libraries

Advantages:

  • There are faster, since all modules are in the same file
  • Their distrubation and insallation are easier

Drawbacks:

  • Use more memory space
  • Slower compilation process

Dynamic libraries

Advantages:

  • Use less memory space
  • Faster compilation process

Drawbacks:

  • Could couse dependecy problems in aplications
  • Compatibilty problems if the library is removed

--

--