// Minimal
cmake -S mySourceTree -B myBuildTree
// With a custom generator
cmake -G myGenerator -S mySourceTree -B myBuildTree
// With some options
cmake -S mySourceTree -B myBuildTree -D MyOption=MyOptionValue
How to list project variables
// List non advanced variables
cmake -S mySourceTree -B myBuildTree -L
// List advanced variables as well
cmake -S mySourceTree -B myBuildTree -LA
// List variables with help messages
cmake -S mySourceTree -B myBuildTree -LAH
How to build a cmake project
// After configuring your cmake project
cmake --build myBuildTree
// With multithreading enabled
cmake --build myBuildTree --parallel myNumberOfJobs
// Building a specific target
cmake --build myBuildTree --target myTarget
// Building a specific configuration [Release, Debug, ..].
// CMake uses Debug as default one
cmake --build myBuildTree --config myConfig
// Building but cleanning the old build first
cmake --build myBuildTree --clean-first
How to make a proper installation of a cmake project
// After building your cmake project
cmake --install myBuildTree
// Installing a specific configuration [Release, Debug, ..]
cmake --install myBuildTree --config myConfig
// Installing in a custom directory
cmake --install myBuildTree --prefix myInstallDirectory
How to open a project
// After configuring your cmake project
// This command will automatically launch visual studio
cmake --open myBuildTree
How to make a branch control structure
# Conditional blockif (<condition>)
<commands>
elseif (<condition>)
<commands>
else ()
<commands>
endif ()
# Logical operators
NOT <condition>
<condition> AND <condition>
<condition> OR <condition>
How to create / call a function
function (myFunctionName myFunctionArg0 myFunctionArg1)
message ("${myFunctionArg0}${myFunctionArg1}")
endfunction ()
myFunctionName("arg0""arg1")
How to set a cmake minimum version
cmake_minimum_required(VERSION <minimum-version>)
How to set a project name, version, description, URL and language
# Project version, description, url and language are optional. # These informations will be used when you will package your app.project(myProject \
VERSION <your project version here in the form <major>[.<minor>[.<patch>[.<tweak>]]]> \
DESCRIPTION <your project description here> \
HOMEPAGE_URL <your project URL here> \
LANGUAGES <your project language here. Must be one of C, CXX, CUDA, OBJC, OBJCXX, Fortran, HIP, ISPC, ASM>)
How to create / set a variable
# Setting a normal variableset(myVariable myValue)
# Setting an environment variableset(ENV{myVariable} myValue)
# Setting a cache variable# type could be: BOOL, FILEPATH, PATH, STRING, INTERNAL# Use FORCE to overwrite the value already present in the caheset(myVariable myValue CACHE <type> <docstring> [FORCE])
How to destroy / unset a variable
# Unset a normal variableunset(myVariable)
# Unset an environment variableunset(ENV{myVariable})
# Unset a cache variableunset(myVariable CACHE)
How to reference a variable
# To retrieve the value of a variable, type ${myVariable}# To retrieve the value of an environment variable, type $ENV{myVariable}# To retrieve the value of a cached variable, type
$CACHE{myVariable}
# Use STATIC to create a static library (.lib, .a)# Use SHARED to create a shared library (.dll, .so)add_library(myLibrary [STATIC | SHARED] [<source> ...])
How to prevent building in the source directory
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR"\n""In-source builds are not allowed.\n""Instead, provide a path to build tree like so:\n""cmake -B <destination>\n""\n""To remove files you accidentally created execute:\n""rm -rf CMakeFiles CMakeCache.txt\n"
)
endif()
How to add a subdirectory
add_subdirectory(mySubdirectory)
How to add a new module path
list(APPENDCMAKE_MODULE_PATH myModulePath)
How to include a module
include(myModule)
How to create an object file
# Object files are .obj files produced by the compileradd_library(myObjectFile OBJECT [<source> ...])
How to link against an object file
# Because object files are generated when building your app, you have to use generator expression to link them against an executable or libraryadd_library(myLibrary $<TARGET_OBJECTS:myObjectFile>)
add_executable(myExecutable $<TARGET_OBJECTS:myObjectFile>)
# Here, no more generator expression used because your are linking against a targettarget_link_libraries(myTarget myObjectFile)
# if you want to link an object file against a shared library, you have to add this line tooset_target_properties(myObjectFile PROPERTIES POSITION_INDEPENDENT_CODE 1)