Static Libraries \/ Dynamic Libraries

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

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

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.

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

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.

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.

Dynamic Libraries

  • There are part of the run time enviroment (environment in which a program or application is executed).

Advantages and drawbacks of static and dynamic libraries.

Static libraries

Advantages:

  • There are faster, since all modules are in the same file

Drawbacks:

  • Use more memory space

Dynamic libraries

Advantages:

  • Use less memory space

Drawbacks:

  • Could couse dependecy problems in aplications

Programmer

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