To main page..

CMakeLists Cheatsheet


Predefined CMake Functions

cmake_minimum_required (VERSION 3.14)

Define minimal CMake version.

project(project_name)

Names the project / VS solution.

set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

Sets the minimal C++ standard to use and requires it's usage. NOTE: not all IDE's enforce the usage of the standard! VSCode for instance still needs to be configured manually.

file(GLOB_RECURSE MyApp_include ${PROJECT_SOURCE_DIR}/MyApp/include/*.h)

Makes a list of paths to all files satisfying the wildcard in specified directory and it's sub-directories. Supposedly discouraged by modern cmake practices: each file ought to be specified manually but who in their right mind would bother with that for large repos?

add_executable(MyApp ${MyApp_include} ${MyApp_src})

Defines a project that builds a single executable. When using target_xxx() functions on it, specify the PRIVATE keyword since nothing should be linking against an executable.

add_library(MyLib STATIC ${MyLib_include} ${MyLib_src})

Defines a project that builds a single library. Use STATIC as keyword for static .lib's, SHARED for .dll's and .so's .

target_sources(MyApp PRIVATE ${someThirdParty_include} ${someThirdParty_src})

Add additional sources to an existing target.

target_include_directories(MyApp PRIVATE
    ${PROJECT_SOURCE_DIR}/MyLib/include/
    ${PROJECT_SOURCE_DIR}/MyApp/include/
    )

Gives the specified project access to the specified source directories. Use PUBLIC as keyword when the target is a library. Use PRIVATE when the target is an executable since nothing is should be linking against an executable.

target_link_libraries(MyApp PRIVATE
    general MyLib
    optimized ${PROJECT_SOURCE_DIR}/thirdparty/someLib.lib
    debug ${PROJECT_SOURCE_DIR}/thirdparty/someLib_debug.lib
    )

Links the specified target with other libraries, either defined above or specified as paths. Prepend the dependancies with "general" to specify it as a dependancy for all builds. Prepend dependancies with "optimized" to specify it as a dependancy for Release builds. Prepend dependancies with "debug" to specify it as a dependancy for Debug builds.

add_compile_definitions(MY_DEFINE ON)

Add a solution-wide define.

source_group(name FILES sources)

Creates a visual studio filter containing the files specified.

add_compile_definitions(APPLICATION_RESOURCES_DIR=”${PROJECT_SOURCE_DIR}/resources/”)

Create a define for a path to be used in the code.

file(MAKE_DIRECTORY ${PROJECT_SOURCE_DIR}/txtOutputs)

Create a directory.

set_target_properties(Application PROPERTIES RUNTIME_OUTPUT_DIRECTORY “${PROJECT_SOURCE_DIR}/build/MyApp/bin”)

Define where to put the target's executable. For some reason doesn't work for libs...

message(WARNING “You’re not using Microsoft Visual Studio IDE.”)

Write a message for the user. STATUS for simple message, FATAL_ERROR to throw error and stop generation. WARNING for warning.

set_target_properties(MyApp PROPERTIES PREFIX “exe_”)

Appends a prefix to a target's output file. Ex: a target named "MyApp" will build a "exe_MyApp.exe" if "exe_" is specified as a prefix.

find_package(pkg_name optional_kwrd REQUIRED)

Imports a target that can be linked against typically named something like pkg_name::pkg_name . The optional_kwrd is here if you want to force a particular search mode, leave it empty.


Predefined CMake Variables

PROJECT_NAME

Variable that contains name of the solution.

PROJECT_SOURCE_DIR

Variable that holds path to the directory where topmost CMakeLists.txt is located.

CMAKE_BINARY_DIR

Variable that holds path to the directory where cmake was launched from. Typically is "${PROJECT_SOURCE_DIR}/build/" .


Snippets

file(…)
add_xxx(…)
target_xxx(…)

Typical order of calls for creating a target.

if(NOT EXISTS “${PROJECT_SOURCE_DIR}/build/”)
    message(FATAL_ERROR “Please specify an out-of-source directory ‘build/’ in the project’s root directory. If you don’t know what an out-of-source build is, here’s a link: https://cmake.org/cmake/help/book/mastering-cmake/chapter/Getting%20Started.html”)
endif()

Require the user to make an out-of-source CMake build.

if (UNIX AND NOT APPLE)
    set(LINUX TRUE)
endif()

Detect and define linux.

set(USE_EASY_PROFILER OFF CACHE BOOL “Whether to enable profiling with easy_profiler.”)
if (USE_EASY_PROFILER)
    add_compile_definitions(BUILD_WITH_EASY_PROFILER)
endif()

Create a define that can be toggled on and off in the CMake GUI.

file(GLOB_RECURSE executable_files “/main/*.cpp”)
foreach(file “${executable_files}”)
    get_filename_component(executable_name “${file}” NAME_WE)
    add_executable(“${executable_name}” “${file}”)
endforeach()

Make executable for each file in a directory.

if (“ STREQUAL ")
    doThings...
endif()</strong>

Comparing strings.

© 2024 Oleg Loshkin   •  Powered by Jekyll   •   Theme  Moonwalk