Introduction to cmake


Flux Capacitor Library

Command target_link_libraries is used to specify dependencies. Requirements for include directories are defined with command target_include_directories as seen before.

Both commands support the keywords PRIVATE, PUBLIC and INTERFACE to specify the usage requirements:

  • PRIVATE Needed by me, but not my dependants.
  • PUBLIC Needed by me and my dependants.
  • INTERFACE Not needed by me, but by my dependants only.

Attention: Build targets that have source files should always list the files explicitly. Using a mechanism like file globing hides dependencies and prevents the build tools from correctly resolve build dependencies.


add_library(fruit::apple ALIAS apple)

  PUBLIC include

  PRIVATE fruit::seeds
  PUBLIC fruit::edible

Exercise 3

Add library flux_capacitor to the project. As usual also add an alias target named time_machine::flux_capacitor.

Folder structure
+--- CMakeLists.txt
+--- flux_capacitor/
|    +--- CMakeLists.txt
|    +--- include/
|    |    +--- flux_capacitor/
|    |         +--- flux_capacitor.h
|    +--- src/
|         +---
|         +--- capacitor.h
|         +---
|         +---
|         +--- flux.h
+--- ...

The library has three source files: src/, src/ and src/

The library is implementing the time_travel interface. It is used by the library and also part of its own public interface. So the time_machine::time_travel interface is a public dependency.

Also, don't forget to add the project::settings as a private dependency.

The include folder is a public include directory.

Run CMake
Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to