July 26, 2020

Static or shared library C++

Do you know the different kinds of libraries in C/C++? .dll, .lib. .a, .so... All those extensions depend on your OS but can signify the same thing: A static or a shared library.

Static Library - Copy in executable

First, let see the static library. A static library contains a set of routine/function available and copied in the executable binary at linkage time.
In result, this is a binary launchable without any supplementary dependencies! However, due to this series of routine copied, the executable is larger.

Static library creation in Linux (GCC)

In Linux, a static library is named an "archive" (.a).
The creation of a static library is done by the archiver "ar" and not gcc/g++. With the option rcs:

  • "r": Insert the files member... into archive (with replacement). This operation differs from q in that any previously existing members are deleted if their names match those being added.
  • "c"Create the archive. The specified archive is always created if it did not exist, when you request an update. But a warning is issued unless you specify in advance that you expect to create it, by using this modifier. 
  • "s": Write an object-file index into the archive, or update an existing one, even if no other change is made to the archive.
ar -rsc myLibrary.a myObject.o

Static library creation in Windows (MSCV)

In Windows, a static library is named ".lib". Be careful, this is exactly the same extension as the "import library".
The static library with MSCV can be created thanks to the executable "lib":

lib /out:myLibrary.lib myObject.obj
Pros:
  • Easy to launch - No dependencies needed at runtime.
  • Routine copied in executable.
Cons:
  • Executable larger.
  • Routine to load in memory. Slower compared to Shared Library.

Shared Library - Export in separate object

Second kind of library. Comparing to the "Static Library", the routines/functions available are not copied in the executable. Instead, the "Shared Library" will only includes the address of the library (which is light)!
Concerning the functions execution, they are loaded at runtime!

Shared library creation in Linux (GCC)

In Linux, the shared library has the extension ".so" (for "Shared Object"). GCC can create a shared library with the option "-shared":

g++ -shared -o myLibrary.so MyObject.o

During runtime, your OS should be able to access to the .so. You can do this configuration with ldconfig.

Shared library creation in Windows (MSCV)

In Windows, two files are created:

  • .lib, similar to the "static" one but without the function routines. This is the "Import Library", used to import instruction in your executable during linkage.
  • .dll, meaning "Dynamic Link Library". This is the file loaded at runtime.

To export functions in your DLL, you should have the "__dllexport" mark at each function declaration you want to "export". To build a shared library:

link /DLL /OUT:myDLL.dll myLibrary.o
Pros:
  • Smaller executable. Optimized if several executable uses the same library.
  • Faster, shared library code is already in the memory.
Cons:
  • Loaded at runtime, dependency and configuration needed.

Header-Only Library - Neither static nor shared library

Currently, there is another kind of library used in C/C++: The "Header Only". In the fact, Header-Only libraries are not a kind of library.

The name is meaningful, this "library" contains only headers and no shared object or static archive. The power of this kind of library is they can be "OS agnostic" and easier to includes compared to the static and shared one.
However, those advantages are balanced by a longer compilation time and the routine/function copied in the executable. This kind of library is perfect for short functions/classes.

Pros:
  • Easy to include.
  • Can be easily OS agnostic for delivery.
  • Routine copied in executable.
  • Easy to launch - No dependencies needed at runtime.
Cons:
  • Longer compilation time.
  • Not adapted for large library.
  • Executable larger, routine to load in memory.

About the author 

Axel Fortun

​Developer specialized in Linux environment and embedded systems.
​Knowledge in multiple languages as C/C++, Java, Python​ and AngularJs.
​Working as Software developer since 2014 with a beginning in the car industry​ and then in ​medical systems.