Building Different Library Types

Let's learn about the three types of libraries.

In this lesson, we'll discuss three types of libraries. After source code is compiled, we might want to avoid compiling it again for the same platform or even share it with external projects wherever possible. Of course, we could just simply provide all of our object files as they were originally created, but that has a few downsides. It is harder to distribute multiple files and add them individually to a buildsystem. It can be a hassle, especially if they are numerous. Instead, we could simply bring all object files into a single object and share that. CMake helps greatly with this process. We can create these libraries with a simple add_library() command (which is consumed with the target_link_libraries() command). By convention, all libraries have a common prefix, lib, and use system-specific extensions that denote what kind of library they are:

  • A static library has a .a extension on Unix-like systems and .lib on Windows.

  • Shared libraries have a .so extension on Unix-like systems and .dll on Windows.

When building libraries (static, shared, or shared modules), we'll often encounter the name "linking" for this process. Even CMake calls it that in the following build output:

Get hands-on with 1400+ tech skills courses.