Back
Close

Introduction to cmake

raphaelmeyer
28.6K views

Solution Exercise 4

Sample solution to the exercise:

# [project]/flux_capacitor/CMakeLists.txt

# ...

add_subdirectory(tests)
# [project]/flux_capacitor/tests/CMakeLists.txt

add_executable(flux_capacitor-tests
  flux_capacitor.cc
  flux.cc
  capacitor.cc
)
add_executable(tests::flux_capacitor ALIAS flux_capacitor-tests)

target_link_libraries(flux_capacitor-tests
  PRIVATE
    catchorg::catch2
    time_machine::flux_capacitor
    project::settings
)

target_include_directories(flux_capacitor-tests
  PRIVATE ../src
)

add_custom_target(run-flux_capacitor-tests ALL
  COMMAND tests::flux_capacitor
)

ctest

CMake itselfs also provides an infrastructure to add tests to project. There are the commands add_test and enable_testing for adding tests to a project. And there is also a CMake test driver program called ctest.

Depending on your project structure, IDE integration and requirements when you want to run your test, the usage of ctest can be helpful.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io