Differences between static and dynamic libraries.

Steffany Naranjo Vargas
6 min readSep 7, 2020

What is a library and why to use them?

Is a collection of pre-compiled pieces of code called functions. The library contains common functions and together, they form a package called — a library. Functions are blocks of code that get reused throughout the program. Using the pieces of code again in a program saves time. It keeps the programmer from rewriting the code several times.

In programming context library is something which has some sort of that code which is pre compiled and could get reused in any program for some specific functionality or feature.

Now on the basis of execution and storage of this code library is classified in two types Static library and Shared library.

Static libraries

Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime. This last comment seems obvious, as we already know that object files are also used only during the linking phase, and are not required during runtime — only the program’s executable file is needed in order to run the program.

As we may know, when you compile a source file you get an object file. Its extension may be .o. A static library is basically a collection of object files, kind of like a .zip file but probably not compressed. The linker, when trying to generate an executable tries to resolve the referenced symbols, locate in which object file (be it in a library or otherwise) they are defined and links them together. So, a static library may also contain an index of defined symbols in order to facilitate this.

How to create a static library?

  1. You have to create the header file with all the prototypes function that you will use, then you have to crear all the .c files with the functions
  2. You have to compile all the .c files ir order to get objectives files.

Now you will have a copy of yours .c files with all the .o extensions.

3. In order to create a library you will use this command.

Now the library will be created with the name that you choose

4. After an archive is created, or modified, there is a need to index it.

To remember: On some systems, the archiver (which is not always ar) already takes care of the index, so ranlib is not needed (for example, when Sun's C compiler creates an archive, it is already indexed). However, because 'ar' and 'ranlib' are used by many makefiles for many packages, such platforms tend to supply a ranlib command that does nothing. This helps using the same makefile on both types of platforms.

Use a static library

To invoke the library we have to use the compiler gcc in this way

Dynamic libraries

A Dynamic Library is a collection of object files that can be linked to any program at run-time by inserting the location of the function in memory to the executable. This provides a way for code to be used even though it could be loaded anywhere in memory. Once a dynamic library is loaded into memory, it’s code can be used by any program that needs it. This is much different than Static Libraries and keeps the size of executables low. Because Dynamic Libraries provide a way to use code that’s anywhere in memory, if the code is updated, you don’t need to recompile. Just because the code was updated, doesn’t necessarily mean it’s location has been changed, so the library still knows where to direct the program. This means that updating code is much simpler and requires less time.

How to create a dynamic library?

  1. You will have to make a new directory with the header and all the .c files with the functions that you will use.

2. To convert the files in object files and create the dynamic library you have to use the next command

Now you will be able to use the library

-fPIC: The .c source files need to be prepared for use in a dynamic library. Since multiple programs can all use one instance of a dynamic library, the library can’t store data at fixed addresses. This is because the location of the library in memory will vary between programs. This is done by using the compiler flag -fPIC. Since we need to apply this step after the compile process has generated the object code, the compiler must be told to halt and return one object file (.o) for each source file. This is done by using the -c flag.

-shared: The object files are now ready to be compiled into a dynamic library. This is done by compiling all of the .o files

How to use a dynamic library

The program needs to know where to look for library files, we must add that location to the environmental variable in this way.

Difference between static and dynamic libraries

As we see, we have a lot of difference in how to make this libraries but also we will find more difference.

Import.

  • Static library: Are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder.
  • Dynamic libraries: Are get import at the time of execution of target program by the Operating system itself.

Size.

  • Static library: Are large in size as because external programs are built in the executable file.
  • Dynamic library: Are much smaller as because there is only one copy of dynamic library that is kept in memory at the time of execution only otherwise its location is remote.

Compilation.

  • Static library: Recompilation is required if any changes were applied to external files.
  • Dynamic library: There is no need to recompile the executable.

Performance.

  • Static library: Take longer to execute, because loading into the memory happens every time while executing.
  • Dynamic library: Are faster because shared library code is already in the memory.

Compatibility.

  • Static library: No compatibility issue has been observed.
  • Dynamic library: has compatibility issue as target program will not work if library gets removed from the system.

This is a short table about some extra difference between this libraries.

Advantages and drawbacks.

Static library advantage.

  • Zero compatibility issues.
  • Speed.

Static library drawbacks.

  • Take a lot of time.
  • The size is too large.

Dynamic library advantage.

  • Linking happen at run time.
  • Only one copy of the shared library is kept in memory.

Dynamic library drawbacks.

  • Compatibility issues.
  • Loading time is more.

If you want more information about libraries you can click here or here.

--

--