This article contains useful and frequently-used commands and variables when using cmake
build system, as well as their corresponding meanings.
Commands
find_package(CUDAToolkit REQUIRED)
: locate NVIDIA CUDA toolkit and associated libraries without enabling CUDA language support globally, detailsinclude(CheckLanguage) check_language(CUDA)
: check CUDA language statusset_source_files_properties(FILES PROPERTIES LANGUAGE CUDA)
: forcibly set language property for source files, which determines the compiler used to compile themselves
Variables
CMAKE_VERBOSE_MAKEFILE:BOOL
: demonstrate build commands when executingmake
PROJECT_SOURCE_DIR
: directory where the top-most levelCMakeLists.txt
containingproject()
command; NOT affect byadd_subdirectory()
CMAKE_MODULE_PATH
: semicolon separated directories used to search CMake modules to be loaded byfind_package()
orinclude()
; need to be set by project. Commonly the CMake module files will be placed in thecmake
folder of the project.CMAKE_PREFIX_PATH
: A path list which specifies the search path forfind_path()
,find_library()
orfind_program()
. Suffices like/lib
,/bin
will be appended for the corresponding routine.CMAKE_{LANG}_COMPILER
:LANG
could beC
,CXX
orCUDA
depending on the project language. Besides, there is one more compiler option for CUDA:CMAKE_CUDA_HOST_COMPILER
, which determines the one used to compile host part code in CUDA source file.CMAKE_EXPORT_COMPILE_COMMANDS
: if enabled,compile_commands.json
will be generated in the build folder.
CMake Cache
Officlal docs: https://cmake.org/cmake/help/book/mastering-cmake/chapter/CMake%20Cache.html
CMake cache variable are stored in CMakeCache.txt
file when you configure the CMake-based project firstly. Later runs will not evaluate them every time, instead, CMake just read their values from the cache file. Besides, every CMake cache variable has a global scope.
If you set the file via command-line -D
option, the cache value will be overwritten forcibly.
Properties
- Source file language: by default, CMake identifies the language as well as the compiler and linker used to compile each compile unit by the source file extension. If the source file ends up with
.cc
, CMake will useCXX
compiler to compile it. There are some exceptions that the target language and the source file extension are mis-matched. We can use CMake commandset_source_files_properties(${file_glob} PROPERTIES LANGUAGE CUDA)
to change the target language manually.