Quick Start

target_include_directories(target PRIVATE ${directories})
target_link_libraries(target ${library_paths})
target_link_options(target PRIVATE LINKER:-rpath,${library_path})

Variables

Tutorial

  • https://cliutils.gitlab.io/modern-cmake/

Directory Variables

  • CMAKE_SOURCE_DIR: Top-level CMakeLists.txt file’s dir.
  • CMAKE_CURRENT_SOURCE_DIR: This CMakeLists.txt files’ dir.
  • PROJECT_SOURCE_DIR: Most recent CMakeLists.txt file that defines a project
  • projectName_SOURCE_DIR: Directory of the projectName
  • For binary directories, replace SOURCE with BINARY

Snippets

Iterate all targets

https://stackoverflow.com/questions/37434946/how-do-i-iterate-over-all-cmake-targets-programmatically

function(get_all_targets var)
    set(targets)
    get_all_targets_recursive(targets ${CMAKE_CURRENT_SOURCE_DIR})
    set(${var} ${targets} PARENT_SCOPE)
endfunction()

macro(get_all_targets_recursive targets dir)
    get_property(subdirectories DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
    foreach(subdir ${subdirectories})
        get_all_targets_recursive(${targets} ${subdir})
    endforeach()

    get_property(current_targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
    list(APPEND ${targets} ${current_targets})
endmacro()

get_all_targets(all_targets)
message("All targets: ${all_targets}")

Custom commands

  • https://stackoverflow.com/questions/18427877/add-custom-build-step-in-cmakehttps://dev.to/iblancasa/learning-cmake-3-understanding-addcustomcommand-and-addcustomtarget-43gp
  • https://stackoverflow.com/questions/18427877/add-custom-build-step-in-cmake