Introduction to cmake

raphaelmeyer
1,669 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Solution Exercise 2

Here is a sample solution of the CMakeLists.txt for the time travel interface library.

# [project]/time_travel_interface/CMakeLists.txt

add_library(time_travel INTERFACE)
add_library(time_machine::time_travel ALIAS time_travel)

target_include_directories(time_travel INTERFACE include)

Project settings

Interface libraries can also be used to define project settings. For example the required C++ standard or additional compiler flags.

Attention: Only define compiler flags if you have a really good reason. Adding compiler flags will break the portability and compatibility of your project.

Example:

[project]/
+--- CMakeLists.txt
+--- settings/
|    +--- CMakeLists.txt
+--- ...     
# [project]/settings/CMakeLists.txt

add_library(settings INTERFACE)
add_library(project::settings ALIAS settings)

target_compile_features(settings INTERFACE cxx_std_17)

target_compile_options(settings
  INTERFACE
    "-Wall"
    "-Wextra"
    "-Werror"
)

In addition to CMake commands target_compile_features and target_compile_options from the example, there is also a command target_compile_definitions.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content