TExcelTAD
Home › Modern CMake (with CTest & CPack) › Libraries: STATIC, SHARED, OBJECT, INTERFACE

Libraries: STATIC, SHARED, OBJECT, INTERFACE

add_library() creates a library target. The keyword after the name says what kind:

Kind Produces (Linux) When to use
STATIC libname.a, an archive of object files copied into each program that links it the default choice for internal libraries
SHARED libname.so (Windows .dll + import .lib, macOS .dylib), loaded at run time code shared by several programs, plugins hosts, distributing a binary SDK
MODULE a .so that is never linked, only dlopened plugins
OBJECT no file, just a set of .o files other targets can use compile once, use the objects in several targets
INTERFACE nothing is built header-only libraries, or a bundle of settings (flags, definitions)

If you leave the keyword out, the variable BUILD_SHARED_LIBS decides: ON means SHARED, otherwise STATIC. That lets the person building choose with -DBUILD_SHARED_LIBS=ON, which is why libraries meant for others usually omit the keyword.

All five in one project

bash · try it yourself
mkdir -p kinds && cd kinds
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Kinds LANGUAGES CXX)

add_library(mathstatic STATIC math.cpp)
add_library(mathshared SHARED math.cpp)
add_library(plugin MODULE plugin.cpp)
add_library(common OBJECT common.cpp)
add_library(config INTERFACE)
target_compile_definitions(config INTERFACE APP_NAME="kinds")

add_executable(app main.cpp)
target_link_libraries(app PRIVATE mathstatic common config)

foreach(t mathstatic mathshared plugin common config app)
  get_target_property(type ${t} TYPE)
  message("${t}: ${type}")
endforeach()
CM
echo 'int twice(int x) { return 2 * x; }' > math.cpp
echo 'extern "C" int plugin_version() { return 3; }' > plugin.cpp
echo 'const char* banner() { return "== kinds =="; }' > common.cpp
cat > main.cpp <<'SRC'
#include <cstdio>
int twice(int);
const char* banner();
int main() { std::printf("%s %s: twice(21) = %d\n", banner(), APP_NAME, twice(21)); }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE
cmake --build build
./build/app
ls build | grep -E '\.(a|so)
  

mathstatic: STATIC_LIBRARY
mathshared: SHARED_LIBRARY
plugin: MODULE_LIBRARY
common: OBJECT_LIBRARY
config: INTERFACE_LIBRARY
app: EXECUTABLE
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/kinds/build
[1/9] Building CXX object CMakeFiles/mathstatic.dir/math.cpp.o
[2/9] Linking CXX static library libmathstatic.a
[3/9] Building CXX object CMakeFiles/mathshared.dir/math.cpp.o
[4/9] Building CXX object CMakeFiles/plugin.dir/plugin.cpp.o
[5/9] Building CXX object CMakeFiles/common.dir/common.cpp.o
[6/9] Building CXX object CMakeFiles/app.dir/main.cpp.o
[7/9] Linking CXX shared library libmathshared.so
[8/9] Linking CXX shared module libplugin.so
[9/9] Linking CXX executable app
== kinds == kinds: twice(21) = 42
libmathshared.so
libmathstatic.a
libplugin.so

Look at what got built:

  • mathstatic became libmathstatic.a, mathshared became libmathshared.so, and plugin became libplugin.so (MODULE libraries get the same name pattern but can't be linked).
  • common (OBJECT) produced only common.cpp.o, and app linked that object file directly.
  • config (INTERFACE) built nothing; linking it gave app the APP_NAME definition. An INTERFACE library is how you package settings as a target.

Header-only libraries

A header-only library is an INTERFACE library with an include directory:

cmake
add_library(ringbuf INTERFACE)
target_include_directories(ringbuf INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_features(ringbuf INTERFACE cxx_std_20)

Every target that links ringbuf gets the include path and C++20, and nothing is compiled until a consumer includes the header.

ALIAS targets and namespaces

Libraries installed as packages are used through namespaced names like fmt::fmt or ZLIB::ZLIB. Give your in-tree libraries the same name with an ALIAS, so consumers write the same target_link_libraries line whether the library comes from the same build or from find_package:

cmake
add_library(geometry src/area.cpp)
add_library(Shapes::geometry ALIAS geometry)
# elsewhere:
target_link_libraries(app PRIVATE Shapes::geometry)

The :: has a second benefit: CMake knows a name with :: must be a target, so a typo is an error at generate time instead of a mysterious -lShapes::geometry link failure:

bash · try it yourself
mkdir -p typo && cd typo
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Typo LANGUAGES CXX)
add_library(geometry area.cpp)
add_library(Shapes::geometry ALIAS geometry)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE Shapes::geometyr)
CM
echo 'int area() { return 1; }' > area.cpp
echo 'int main() { return 0; }' > main.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE
-- Configuring done (0.2s)
CMake Error at CMakeLists.txt:6 (target_link_libraries):
  Target "app" links to:

    Shapes::geometyr

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.



-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.

Compare with linking a plain misspelled name, which CMake must assume is a system library (-lgeometyr), so the error only shows up at link time, from the linker. Use :: names whenever you can.

BUILD_SHARED_LIBS

bash · try it yourself
mkdir -p choose && cd choose
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Choose LANGUAGES CXX)
add_library(core core.cpp)          # no STATIC/SHARED: BUILD_SHARED_LIBS decides
get_target_property(type core TYPE)
message("core is a ${type}")
CM
echo 'int core() { return 1; }' > core.cpp
cmake -S . -B build-a -G Ninja --log-level=NOTICE 2>&1 | grep -v "^--"
cmake -S . -B build-b -G Ninja --log-level=NOTICE -DBUILD_SHARED_LIBS=ON 2>&1 | grep -v "^--"
core is a STATIC_LIBRARY
core is a SHARED_LIBRARY

Test yourself

Which library kind builds nothing and exists only to pass include paths or flags to its consumers?

  1. STATIC
  2. OBJECT
  3. INTERFACE
  4. MODULE

add_library(core core.cpp) without a kind keyword. What decides STATIC vs SHARED?

  1. Always STATIC
  2. The BUILD_SHARED_LIBS variable
  3. The file extension
  4. The generator

Why link Shapes::geometry rather than geometry?

  1. It is faster
  2. A name with :: must be a target, so a typo is caught at generate time; and the name matches the installed package
  3. Plain names don't work
  4. It makes the library shared