C++ Libraries — Part II: Implementation

Inbal Levi
Nerd For Tech
Published in
4 min readFeb 9, 2021

--

This blog post is a continuation of C++ Libraries — Part I: Design

In this part, we’ll go over the basics of creating a C++ library.

II. Techniques for creating code for a C++ library

C++ provides multiple ways to create the library code:

(I) and (IV) can be packaged into a package (.o / .a / .so), whereas (II) and (III) have to be provided as source code.

III. Techniques for packaging the library code

(Notice: The rest of the blog will explain the technical details of creating a library. The examples and instructions assume a Linux-based OS using CMake tool, Windows-based OS have a very similer process)

As mentioned in detail in the “Code composition” section above, generally speaking, there are three main ways for the user to consume the library:

  1. Use library sources — This is commonly used with option (III) above, since templates can be used to create code `on demand`, but you could just as easily package sources into a library.
  2. Link with the library statically — The library code will be embedded as part of the use program (library code will be part of the binary).
    The library can be provided as:
    - An Object file (.o)
    - A collection of object files (an “Object library”)
    - A collection of object files packaged together with an “index” file (.a)
    (an “Archive library”)
  3. Link with the library dynamically — The user code will have references to the library code (symbols). when running the user code, the loader will load the library to RAM and provide those references.
    In this case, what defines the composition is the symbols (ABI — Application binary interface) provided by the library.
    If the library fails to provide them (on compilation time, spotted by the linker, or on loading time, spotted by the loader) we’ll get a linker / loader error.

Additional types exist, but they will not be discussed in this post.

All of the options above can be configured at the CMakeLists file of the library Project (or folder)

  1. For sources library — create “INTERFACE” library.

2. Create an object / static library:

.o (object library)

.a (static library — archive)

3. Create a dynamic library:

.so (shared object)

For more details and technical specifications on how to create each option manually in gcc, see: https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

IV. Composing the library with user code

This is the process of creating the main.out executable.

Composing sources library with user code

When compiling sources, “main.out” is the only file that needs to be on the target.

Composing a library as objects (static / dynamic lib)

(*) IMPORTANT: In case of .so, main.out will not contain the binary of the library. Instead, the loader will load the .so to RAM when main.out is executed.

V. Ways of delivering your new library

C++ provides multiple whys to deliver a library (partial list):

(*) IMPORTANT: Notice that the difference between (III) and (IV) is only on the intended usage, as appears in the main file (and not on the library creation).

Comparison between the options above:

Min size: The size of only the part of code which is used by the user (if separated into .o files — only the .o contain functions called by user)

Max size: The size of all the code in the library (since dynamic linking doesn’t know on which part of the code the user will use, the .so have to contain all the code)

Links with additional info relevant to the table:

VI. Epilogue

There are, of course, additional topics to be addressed, this blog tries to cover the most basic and common techniques.

C++20 supports new form of structuring of “composable” code, which can change process of building libraries, especially — remove the need of a header. This form is called modules. This is a whole different topic which deserves its own post.

Thanks to Hana Dusíková and Billy Baker for reviewing this post.

And thank you for reading, I hope you find this blog post beneficial. :)

Update (March 2022): I’ve created TestCMake repo with simplified examples for CMake files (largely aligned with the official tutorial) for Static and Header-only libraries.

--

--

Inbal Levi
Nerd For Tech

C++ Enthusiast. ISO WG21 member, and head of Israel C++ mirror committee.