CMake 教程1

简介: CMake 教程

cmake 命令

# 生成项目构建系统  Generate a Project Buildsystem
 cmake [<options>] <path-to-source>
 cmake [<options>] <path-to-existing-build>
 cmake [<options>] -S <path-to-source> -B <path-to-build>
 
# 编译项目  Build a Project
 cmake --build <dir> [<options>] [-- <build-tool-options>]
 
# 安装编译后的工程  Install a Project
 cmake --install <dir> [<options>]
 
# 打开项目  Open a Project
 cmake --open <dir>
 
# 运行脚本  Run a Script
 cmake [{-D <var>=<value>}...] -P <cmake-script-file>
 
# 运行cmake内置命令行工具  Run a Command-Line Tool
 cmake -E <command> [<options>]
 
# 运行Find-Package工具 Run the Find-Package Tool
 cmake --find-package [<options>]
 
# 查看帮助  View Help
 cmake --help[-<topic>]

生成项目构建系统

 cmake [<options>] <path-to-source>
 cmake [<options>] <path-to-existing-build>
 cmake [<options>] -S <path-to-source> -B <path-to-build>

命令行

源目录

构建目录

cmake src

src

当前目录

cmake build(包含CMakeCache.txt)

从缓存中加载

build

cmake -S src

src

当前目录

cmake -S src build

src

build

cmake -S src -B build

src

build

cmake -B build

当前目录

build

cmake -B build src

src

build

cmake -B build -S src

src

build

cmake -G "Visual Studio 16 2019" -A x64 ../ --fresh

cmake(1) — CMake 3.24.0-rc3 Documentation

编译项目

cmake --build <dir> [<options>] [-- <build-tool-options>]

参数:

--parallel [<jobs>], -j [<jobs>]
    构建时使用的最大并发进程数。如果<jobs>省略,则使用本机构建工具的默认编号。
    这CMAKE_BUILD_PARALLEL_LEVEL如果设置了环境变量,则在未给出此选项时指定默认的并行级别。
 
--target <tgt>..., -t <tgt>...
    构建<tgt>,取代构建默认目标。可以给出多个目标,用空格分隔。
 
--config <cfg>
    对于多配置工具,选择配置<cfg>。
 
--clean-first
    先构建目标clean,然后构建。(仅清洁,使用。)--target clean

常用命令示例:

#windows:
cmake --build . --config Release
 
#linux:
cmake .. -DCMAKE_BUILD_TYPE=Release

安装编译后的文件

cmake --install <dir> [<options>]

参数:

--install <dir>
    要安装的项目二进制目录。这是必需的,并且必须是第一个。
 
--config <cfg>
    对于多配置生成器,请选择配置<cfg>。
 
--component <comp>
    基于组件的安装。只安装组件<comp>。
 
--default-directory-permissions <permissions>
默认目录安装权限。格式的权限<u=rwx,g=rx,o=rx>。
 
--prefix <prefix>
    覆盖安装前缀,CMAKE_INSTALL_PREFIX.
 
--strip
    安装前精简,去掉调试信息等。

cmake 构建系统 cmake-buildsystem

定义可执行文件:  add_executable()

定义可执行库: add_library()

定义二进制target的依赖关: target_link_libraries()

定义共享库类型: BUILD_SHARED_LIBS

CMAKE_CONFIGURATION_TYPES 指定只生成指定的配置

# 只生成 Release 的配置 (多配置的情况下)
cmake ../ -DCMAKE_CONFIGURATION_TYPES=Release
 
# 生成
cmake --build ./
 
# 因为只生成了 Release 的配置,所以报错说没有 Debug|x64 的配置
# C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppBuild.targets(437,5): error MSB8013: 此项目不包含配置和平台组合 Debug|x64。 
 
# 改成 生成 Release
cmake --build ./ --config Release
 
# 生成 bin 成功

cmake 命令

cmake命令 分为:

脚本命令(Scripting Commands),

项目命令(Project Commands),

CTest 命令(CTest Commands),

不推荐使用的命令 (Deprecated Commands)

脚本命令(Scripting Commands)

break

break — CMake 3.24.0-rc3 Documentation

# 从 foreach 或 while 循环中跳出
break()

cmake_host_system_information

cmake_host_system_information — CMake 3.24.0-rc3 Documentation

# Query host system specific information
# 查询主机系统特定信息
  cmake_host_system_information(RESULT <variable> QUERY <key> ...)
 
# Query Windows registry
# 查询 Windows 注册表
  cmake_host_system_information(RESULT <variable> QUERY WINDOWS_REGISTRY <key> ...)
 
 
# 1
cmake_host_system_information(RESULT PRETTY_NAME QUERY DISTRIB_PRETTY_NAME)
message(STATUS "${PRETTY_NAME}")
 
# 2
cmake_host_system_information(RESULT result QUERY WINDOWS_REGISTRY "HKLM")
cmake_host_system_information(RESULT result QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Kitware")
cmake_host_system_information(RESULT result QUERY WINDOWS_REGISTRY "HKCU\\SOFTWARE\\Kitware")

cmake_language

cmake_language — CMake 3.24.0-rc3 Documentation

# 1 ##########################################################################
# 调用 CMake 元操作命令
# 调用命令
cmake_language(CALL <command> [<arg>...])
 
# 示例
set(message_command "message")
cmake_language(CALL ${message_command} STATUS "Hello World!")
# 相当于
message(STATUS "Hello World!")
 
# 2 ##########################################################################
# 评估代码
cmake_language(EVAL CODE <code>...)
 
 
# 3 ##########################################################################
# 延迟调用
cmake_language(DEFER <options>... CALL <command> [<arg>...])
 
# 示例
cmake_language(DEFER CALL message "${deferred_message}")
cmake_language(DEFER ID_VAR id CALL message "Canceled Message")
cmake_language(DEFER CANCEL_CALL ${id})
message("Immediate Message")
set(deferred_message "Deferred Message")
 
# 4 ##########################################################################
# 依赖提供者
cmake_language(SET_DEPENDENCY_PROVIDER <command> SUPPORTED_METHODS <methods>...)

cmake_minimum_required

cmake_minimum_required — CMake 3.24.0-rc3 Documentation

# 定义需要的 最低的 cmake 版本
cmake_minimum_required(VERSION <min>[...<policy_max>] [FATAL_ERROR])
 
# 示例
cmake_minimum_required(VERSION 3.9)

cmake_parse_arguments

cmake_parse_arguments — CMake 3.24.0-rc3 Documentation

# 解析函数或宏参数
cmake_parse_arguments(<prefix> <options> <one_value_keywords>
                      <multi_value_keywords> <args>...)
 
cmake_parse_arguments(PARSE_ARGV <N> <prefix> <options>
                      <one_value_keywords> <multi_value_keywords>)

cmake_path

cmake_path — CMake 3.24.0-rc3 Documentation

# 该命令处理构建系统(即主机平台)格式的路径,而不是目标系统
# 分解 Decomposition
  cmake_path( GET <path-var> ROOT_NAME <out-var>)
  cmake_path( GET <path-var> ROOT_DIRECTORY <out-var>)
  cmake_path( GET <path-var> ROOT_PATH <out-var>)
  cmake_path( GET <path-var> FILENAME <out-var>)
  cmake_path( GET <path-var> EXTENSION [LAST_ONLY] <out-var>)
  cmake_path( GET <path-var> STEM [LAST_ONLY] <out-var>)
  cmake_path( GET <path-var> RELATIVE_PART <out-var>)
  cmake_path( GET <path-var> PARENT_PATH <out-var>)
 
# 查询 Query
  cmake_path( HAS_ROOT_NAME <path-var> <out-var>)
  cmake_path( HAS_ROOT_DIRECTORY <path-var> <out-var>)
  cmake_path( HAS_ROOT_PATH <path-var> <out-var>)
  cmake_path( HAS_FILENAME <path-var> <out-var>)
  cmake_path( HAS_EXTENSION <path-var> <out-var>)
  cmake_path( HAS_STEM <path-var> <out-var>)
  cmake_path( HAS_RELATIVE_PART <path-var> <out-var>)
  cmake_path( HAS_PARENT_PATH <path-var> <out-var>)
  cmake_path( IS_ABSOLUTE <path-var> <out-var>)
  cmake_path( IS_RELATIVE <path-var> <out-var>)
  cmake_path( IS_PREFIX <path-var> <input> [NORMALIZE] <out-var>)
  cmake_path(比较<input1> <OP> <input2> <out-var>)
 
# 修改 Modification
  cmake_path( SET <path-var> [NORMALIZE] <input>)
  cmake_path( APPEND <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  cmake_path( APPEND_STRING <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
  cmake_path( REMOVE_FILENAME <path-var> [OUTPUT_VARIABLE <out-var>])
  cmake_path( REPLACE_FILENAME <path-var> <input> [OUTPUT_VARIABLE <out-var>])
  cmake_path( REMOVE_EXTENSION <path-var> [LAST_ONLY] [OUTPUT_VARIABLE <out-var>])
  cmake_path( REPLACE_EXTENSION <path-var> [LAST_ONLY] <input> [OUTPUT_VARIABLE <out-var>])
 
# 生成 Generation
  cmake_path( NORMAL_PATH <path-var> [OUTPUT_VARIABLE <out-var>])
  cmake_path( RELATIVE_PATH <path-var> [BASE_DIRECTORY <input>] [OUTPUT_VARIABLE <out-var>])
  cmake_path( ABSOLUTE_PATH <path-var> [BASE_DIRECTORY <input>] [NORMALIZE] [OUTPUT_VARIABLE <out-var>])
 
# 本机转换 Native Conversion
  cmake_path( NATIVE_PATH <path-var> [NORMALIZE] <out-var>)
  cmake_path(转换<输入> TO_CMAKE_PATH_LIST <输出变量> [NORMALIZE])
  cmake_path( CONVERT <input> TO_NATIVE_PATH_LIST <out-var> [NORMALIZE])
 
# 散列 Hashing
  cmake_path( HASH <path-var> <out-var>)


cmake_policy

cmake_policy — CMake 3.24.0-rc3 Documentation

configure_file

configure_file — CMake 3.24.0-rc3 Documentation

# 将文件复制到另一个位置并修改其内容。
 
configure_file(<input> <output>
               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
                FILE_PERMISSIONS <permissions>...]
               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
 
# 示例 ###############
# 考虑一个包含foo.h.in文件的源代码树:
#cmakedefine FOO_ENABLE
#cmakedefine FOO_STRING "@FOO_STRING@"
 
# 一个相邻的CMakeLists.txt可以configure_file用来配置标头:
option(FOO_ENABLE "Enable Foo" ON)
if(FOO_ENABLE)
  set(FOO_STRING "foo")
endif()
configure_file(foo.h.in foo.h @ONLY)
 
# foo.h这将在与此源目录对应的构建目录中创建一个。如果该FOO_ENABLE选项打开,配置文件将包含:
#define FOO_ENABLE
#define FOO_STRING "foo"
 
# 否则它将包含:
/* #undef FOO_ENABLE */
/* #undef FOO_STRING */
 
# 然后可以使用include_directories()命令指定输出目录为头文件搜索路径
 
include_directories(${CMAKE_CURRENT_BINARY_DIR})
 
# 以便源可以将标头包含为 #include <foo.h>

continue

continue — CMake 3.24.0-rc3 Documentation

continue()

else

else — CMake 3.24.0-rc3 Documentation

else([<condition>])

endforeach

endforeach — CMake 3.24.0-rc3 Documentation

endforeach([<loop_var>])

endfunction

endfunction — CMake 3.24.0-rc3 Documentation

endfunction([<name>])

endif

endif — CMake 3.24.0-rc3 Documentation

endif([<condition>])

endmacro

endmacro — CMake 3.24.0-rc3 Documentation

endmacro([<name>])

endwhile

endwhile — CMake 3.24.0-rc3 Documentation

endwhile([<condition>])

execute_process

execute_process — CMake 3.24.0-rc3 Documentation

# 执行一个或多个子进程。
 
execute_process(COMMAND <cmd1> [<arguments>]
                [COMMAND <cmd2> [<arguments>]]...
                [WORKING_DIRECTORY <directory>]
                [TIMEOUT <seconds>]
                [RESULT_VARIABLE <variable>]
                [RESULTS_VARIABLE <variable>]
                [OUTPUT_VARIABLE <variable>]
                [ERROR_VARIABLE <variable>]
                [INPUT_FILE <file>]
                [OUTPUT_FILE <file>]
                [ERROR_FILE <file>]
                [OUTPUT_QUIET]
                [ERROR_QUIET]
                [COMMAND_ECHO <where>]
                [OUTPUT_STRIP_TRAILING_WHITESPACE]
                [ERROR_STRIP_TRAILING_WHITESPACE]
                [ENCODING <name>]
                [ECHO_OUTPUT_VARIABLE]
                [ECHO_ERROR_VARIABLE]
                [COMMAND_ERROR_IS_FATAL <ANY|LAST>])

file

file — CMake 3.24.0-rc3 Documentation

# 文件操作命令
# 此命令专用于需要访问文件系统的文件和路径操作
# Reading  读取
  file(READ <filename> <out-var> [...])
  file(STRINGS <filename> <out-var> [...])
  file(<HASH> <filename> <out-var>)
  file(TIMESTAMP <filename> <out-var> [...])
  file(GET_RUNTIME_DEPENDENCIES [...])
 
# Writing  写入
  file({WRITE | APPEND} <filename> <content>...)
  file({TOUCH | TOUCH_NOCREATE} [<file>...])
  file(GENERATE OUTPUT <output-file> [...])
  file(CONFIGURE OUTPUT <output-file> CONTENT <content> [...])
 
# Filesystem  文件系统
  file({GLOB | GLOB_RECURSE} <out-var> [...] [<globbing-expr>...])
  file(MAKE_DIRECTORY [<dir>...])
  file({REMOVE | REMOVE_RECURSE } [<files>...])
  file(RENAME <oldname> <newname> [...])
  file(COPY_FILE <oldname> <newname> [...])
  file({COPY | INSTALL} <file>... DESTINATION <dir> [...])
  file(SIZE <filename> <out-var>)
  file(READ_SYMLINK <linkname> <out-var>)
  file(CREATE_LINK <original> <linkname> [...])
  file(CHMOD <files>... <directories>... PERMISSIONS <permissions>... [...])
  file(CHMOD_RECURSE <files>... <directories>... PERMISSIONS <permissions>... [...])
 
# Path Conversion  路径转换
  file(REAL_PATH <path> <out-var> [BASE_DIRECTORY <dir>] [EXPAND_TILDE])
  file(RELATIVE_PATH <out-var> <directory> <file>)
  file({TO_CMAKE_PATH | TO_NATIVE_PATH} <path> <out-var>)
 
# Transfer  传输
  file(DOWNLOAD <url> [<file>] [...])
  file(UPLOAD <file> <url> [...])
 
# Locking  锁定
  file(LOCK <path> [...])
 
# Archiving  归档
  file(ARCHIVE_CREATE OUTPUT <archive> PATHS <paths>... [...])
  file(ARCHIVE_EXTRACT INPUT <archive> [...])

find_file

find_file — CMake 3.24.0-rc3 Documentation

# 此命令用于查找命名文件的完整路径
# 速记签名是:
 
find_file (<VAR> name1 [path1 path2 ...])
 
find_file (
          <VAR>
          name | NAMES name1 [name2 ...]
          [HINTS [path | ENV var]... ]
          [PATHS [path | ENV var]... ]
          [REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
          [PATH_SUFFIXES suffix1 [suffix2 ...]]
          [DOC "cache documentation string"]
          [NO_CACHE]
          [REQUIRED]
          [NO_DEFAULT_PATH]
          [NO_PACKAGE_ROOT_PATH]
          [NO_CMAKE_PATH]
          [NO_CMAKE_ENVIRONMENT_PATH]
          [NO_SYSTEM_ENVIRONMENT_PATH]
          [NO_CMAKE_SYSTEM_PATH]
          [NO_CMAKE_INSTALL_PREFIX]
          [CMAKE_FIND_ROOT_PATH_BOTH |
           ONLY_CMAKE_FIND_ROOT_PATH |
           NO_CMAKE_FIND_ROOT_PATH]
         )

find_library

find_library — CMake 3.24.0-rc3 Documentation

#速记签名是
find_library (<VAR> name1 [path1 path2 ...])
 
# 该命令用于查找库
find_library (
          <VAR>
          name | NAMES name1 [name2 ...] [NAMES_PER_DIR]
          [HINTS [path | ENV var]... ]
          [PATHS [path | ENV var]... ]
          [REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
          [PATH_SUFFIXES suffix1 [suffix2 ...]]
          [DOC "cache documentation string"]
          [NO_CACHE]
          [REQUIRED]
          [NO_DEFAULT_PATH]
          [NO_PACKAGE_ROOT_PATH]
          [NO_CMAKE_PATH]
          [NO_CMAKE_ENVIRONMENT_PATH]
          [NO_SYSTEM_ENVIRONMENT_PATH]
          [NO_CMAKE_SYSTEM_PATH]
          [NO_CMAKE_INSTALL_PREFIX]
          [CMAKE_FIND_ROOT_PATH_BOTH |
           ONLY_CMAKE_FIND_ROOT_PATH |
           NO_CMAKE_FIND_ROOT_PATH]
         )
 
#  如果指定NO_CACHE,则查找结果缓存到变量VAR中,否则存放在缓存条目中。如果什么都没找到,结果将是<VAR>-NOTFOUND

find_package

find_package — CMake 3.24.0-rc3 Documentation

CMake的find_package指令详解 - 知乎

# 查找包
# 基本用法
find_package(<PackageName> [version] [EXACT] [QUIET] [MODULE]
             [REQUIRED] [[COMPONENTS] [components...]]
             [OPTIONAL_COMPONENTS components...]
             [REGISTRY_VIEW  (64|32|64_32|32_64|HOST|TARGET|BOTH)]
             [NO_POLICY_SCOPE]
             [GLOBAL])
 
 
# 完全用法
find_package(<PackageName> [version] [EXACT] [QUIET]
             [REQUIRED] [[COMPONENTS] [components...]]
             [OPTIONAL_COMPONENTS components...]
             [CONFIG|NO_MODULE]
             [NO_POLICY_SCOPE]
             [GLOBAL]
             [NAMES name1 [name2 ...]]
             [CONFIGS config1 [config2 ...]]
             [HINTS path1 [path2 ... ]]
             [PATHS path1 [path2 ... ]]
             [REGISTRY_VIEW  (64|32|64_32|32_64|HOST|TARGET|BOTH)]
             [PATH_SUFFIXES suffix1 [suffix2 ...]]
             [NO_DEFAULT_PATH]
             [NO_PACKAGE_ROOT_PATH]
             [NO_CMAKE_PATH]
             [NO_CMAKE_ENVIRONMENT_PATH]
             [NO_SYSTEM_ENVIRONMENT_PATH]
             [NO_CMAKE_PACKAGE_REGISTRY]
             [NO_CMAKE_BUILDS_PATH] # Deprecated; does nothing.
             [NO_CMAKE_SYSTEM_PATH]
             [NO_CMAKE_INSTALL_PREFIX]
             [NO_CMAKE_SYSTEM_PACKAGE_REGISTRY]
             [CMAKE_FIND_ROOT_PATH_BOTH |
              ONLY_CMAKE_FIND_ROOT_PATH |
              NO_CMAKE_FIND_ROOT_PATH])

CMake 为包构造了一组可能的安装前缀。在每个前缀下搜索几个目录以查找配置文件。下表显示了搜索的目录。每个条目都适用于遵循 Windows ( W)、UNIX ( U) 或 Apple ( A) 约定的安装树:

/                                                       (W)

/(cmake|CMake)/                                         (W)

/*/                                               (W)

/*/(cmake|CMake)/                                 (W)

/(lib/|lib*|share)/cmake/*/                 (U)

/(lib/|lib*|share)/*/                       (U)

/(lib/|lib*|share)/*/(cmake|CMake)/         (U)

/*/(lib/|lib*|share)/cmake/*/         (W/U)

/*/(lib/|lib*|share)/*/               (W/U)

/*/(lib/|lib*|share)/*/(cmake|CMake)/ (W/U)

在支持 macOS 的系统上FRAMEWORKBUNDLE,在以下目录中搜索包含配置文件的框架或应用程序包:

/.framework/Resources/                    (A)

/.framework/Resources/CMake/              (A)

/.framework/Versions/*/Resources/         (A)

/.framework/Versions/*/Resources/CMake/   (A)

/.app/Contents/Resources/                 (A)

/.app/Contents/Resources/CMake/           (A)


在所有情况下都被视为不区分大小写并对应于任何指定的名称(或由 给出的名称NAMES)。

如果lib/_ CMAKE_LIBRARY_ARCHITECTURE变量已设置。包括一个或lib*多个值lib64lib32或(按该顺序搜索)。libx32lib

find_path

find_path — CMake 3.24.0-rc3 Documentation

# 速记用法
find_path (<VAR> name1 [path1 path2 ...])
 
# 通用用法
find_path (
          <VAR>
          name | NAMES name1 [name2 ...]
          [HINTS [path | ENV var]... ]
          [PATHS [path | ENV var]... ]
          [REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
          [PATH_SUFFIXES suffix1 [suffix2 ...]]
          [DOC "cache documentation string"]
          [NO_CACHE]
          [REQUIRED]
          [NO_DEFAULT_PATH]
          [NO_PACKAGE_ROOT_PATH]
          [NO_CMAKE_PATH]
          [NO_CMAKE_ENVIRONMENT_PATH]
          [NO_SYSTEM_ENVIRONMENT_PATH]
          [NO_CMAKE_SYSTEM_PATH]
          [NO_CMAKE_INSTALL_PREFIX]
          [CMAKE_FIND_ROOT_PATH_BOTH |
           ONLY_CMAKE_FIND_ROOT_PATH |
           NO_CMAKE_FIND_ROOT_PATH]
         )
 
# 此命令用于查找包含命名文件的目录,如果指定NO_CACHE,则查找结果缓存到变量VAR中,否则存放在缓存条目中。如果什么都没找到,结果将是<VAR>-NOTFOUND

find_program

find_program — CMake 3.24.0-rc3 Documentation

# 该命令用于查找程序
# 速记用法
find_program (<VAR> name1 [path1 path2 ...])
 
# 通用用法
find_program (
          <VAR>
          name | NAMES name1 [name2 ...] [NAMES_PER_DIR]
          [HINTS [path | ENV var]... ]
          [PATHS [path | ENV var]... ]
          [REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
          [PATH_SUFFIXES suffix1 [suffix2 ...]]
          [DOC "cache documentation string"]
          [NO_CACHE]
          [REQUIRED]
          [NO_DEFAULT_PATH]
          [NO_PACKAGE_ROOT_PATH]
          [NO_CMAKE_PATH]
          [NO_CMAKE_ENVIRONMENT_PATH]
          [NO_SYSTEM_ENVIRONMENT_PATH]
          [NO_CMAKE_SYSTEM_PATH]
          [NO_CMAKE_INSTALL_PREFIX]
          [CMAKE_FIND_ROOT_PATH_BOTH |
           ONLY_CMAKE_FIND_ROOT_PATH |
           NO_CMAKE_FIND_ROOT_PATH]
         )
 
# 该命令用于查找程序,如果指定NO_CACHE,则查找结果缓存到变量VAR中,否则存放在缓存条目中。如果什么都没找到,结果将是<VAR>-NOTFOUND

foreach

foreach — CMake 3.24.0-rc3 Documentation

foreach(<loop_var> <items>)
  <commands>
endforeach()
 
foreach(<loop_var> RANGE <stop>)
 
foreach(<loop_var> RANGE <start> <stop> [<step>])
 
foreach(<loop_var> IN [LISTS [<lists>]] [ITEMS [<items>]])
 
foreach(<loop_var>... IN ZIP_LISTS <lists>)
 
#############################################################
set(A 0;1)
set(B 2 3)
set(C "4 5")
set(D 6;7 8)
set(E "")
foreach(X IN LISTS A B C D E)
    message(STATUS "X=${X}")
endforeach()
 
# 输出:
-- X=0
-- X=1
-- X=2
-- X=3
-- X=4 5
-- X=6
-- X=7
-- X=8

function

function — CMake 3.24.0-rc3 Documentation

function(<name> [<arg1> ...])
  <commands>
endfunction()

get_cmake_property

get_cmake_property — CMake 3.24.0-rc3 Documentation

get_cmake_property(<var> <property>)
 
#从 CMake 实例获取全局属性。把<property>的值存储在变量<var>中。如果未找到该属性,<var>将设置为NOTFOUND。

get_directory_property

get_directory_property — CMake 3.24.0-rc3 Documentation

get_directory_property(<variable> [DIRECTORY <dir>] <prop-name>)
 
get_directory_property(<variable> [DIRECTORY <dir>]
                       DEFINITION <var-name>)
 
# 将目录范围的属性存储在<variable>变量中

get_filename_component

# 获取完整文件名的特定组成部分
get_filename_component(<var> <FileName> <mode> [CACHE])
 
get_filename_component(<var> <FileName> <mode> [BASE_DIR <dir>] [CACHE])
 
get_filename_component(<var> <FileName> PROGRAM [PROGRAM_ARGS <arg_var>] [CACHE])
 
# 在 3.20 版更改:此命令已被cmake_path()命令取代

get_property

get_property — CMake 3.24.0-rc3 Documentation

# 从范围内的一个对象中获取一个属性
get_property(<variable>
             <GLOBAL             |
              DIRECTORY [<dir>]  |
              TARGET    <target> |
              SOURCE    <source>
                        [DIRECTORY <dir> | TARGET_DIRECTORY <target>] |
              INSTALL   <file>   |
              TEST      <test>   |
              CACHE     <entry>  |
              VARIABLE           >
             PROPERTY <name>
             [SET | DEFINED | BRIEF_DOCS | FULL_DOCS])

if

if — CMake 3.24.0-rc3 Documentation

# 语法
if(<condition>)
  <commands>
elseif(<condition>) # optional block, can be repeated
  <commands>
else()              # optional block
  <commands>
endif()
 
# 基本表达式
if(<constant>)
if(<variable>)
if(<string>)
 
# 逻辑运算符
if(NOT <condition>)
if(<cond1> AND <cond2>)
if(<cond1> OR <cond2>)
if((condition) AND (condition OR (condition)))
 
# 存在性检查
if(COMMAND command-name)
if(POLICY policy-id)
if(TARGET target-name)
if(TEST test-name)
if(DEFINED <name>|CACHE{<name>}|ENV{<name>})
if(<variable|string> IN_LIST <variable>)
 
# 文件操作
if(EXISTS path-to-file-or-directory)
if(file1 IS_NEWER_THAN file2)
if(IS_DIRECTORY path-to-directory)
if(IS_SYMLINK file-name)
if(IS_ABSOLUTE path)
 
# Comparisons
if(<variable|string> MATCHES regex)
if(<variable|string> LESS <variable|string>)
if(<variable|string> GREATER <variable|string>)
if(<variable|string> EQUAL <variable|string>)
if(<variable|string> LESS_EQUAL <variable|string>)
if(<variable|string> GREATER_EQUAL <variable|string>)
if(<variable|string> STRLESS <variable|string>)
if(<variable|string> STRGREATER <variable|string>)
if(<variable|string> STREQUAL <variable|string>)
if(<variable|string> STRLESS_EQUAL <variable|string>)
if(<variable|string> STRGREATER_EQUAL <variable|string>)
 
# 版本比较
if(<variable|string> VERSION_LESS <variable|string>)
if(<variable|string> VERSION_GREATER <variable|string>)
if(<variable|string> VERSION_EQUAL <variable|string>)
if(<variable|string> VERSION_LESS_EQUAL <variable|string>)
if(<variable|string> VERSION_GREATER_EQUAL <variable|string>)
 
# 路径比较
if(<variable|string> PATH_EQUAL <variable|string>)

include

include — CMake 3.24.0-rc3 Documentation

# 从文件或模块加载并运行 CMake 代码
include(<file|module> [OPTIONAL] [RESULT_VARIABLE <var>]
                      [NO_POLICY_SCOPE])

include_guard

include_guard — CMake 3.24.0-rc3 Documentation

# 为 CMake 当前正在处理的文件提供包含保护
include_guard([DIRECTORY|GLOBAL])

list

list — CMake 3.24.0-rc3 Documentation

CMake语法—命令list | 程序员灯塔

Reading
  list(LENGTH <list> <out-var>)
  list(GET <list> <element index> [<index> ...] <out-var>)
  list(JOIN <list> <glue> <out-var>)
  list(SUBLIST <list> <begin> <length> <out-var>)
 
Search
  list(FIND <list> <value> <out-var>)
 
Modification
  list(APPEND <list> [<element>...])
  list(FILTER <list> {INCLUDE | EXCLUDE} REGEX <regex>)
  list(INSERT <list> <index> [<element>...])
  list(POP_BACK <list> [<out-var>...])
  list(POP_FRONT <list> [<out-var>...])
  list(PREPEND <list> [<element>...])
  list(REMOVE_ITEM <list> <value>...)
  list(REMOVE_AT <list> <index>...)
  list(REMOVE_DUPLICATES <list>)
  list(TRANSFORM <list> <ACTION> [...])
 
Ordering
  list(REVERSE <list>)
  list(SORT <list> [...])

macro

macro — CMake 3.24.0-rc3 Documentation

# 开始录制宏以供以后作为命令调用
macro(<name> [<arg1> ...])
  <commands>
endmacro()

mark_as_advanced

mark_as_advanced — CMake 3.24.0-rc3 Documentation

# 将 cmake 缓存变量标记为高级
mark_as_advanced([CLEAR|FORCE] <var1> ...)


CMake 教程2:https://developer.aliyun.com/article/1597129

目录
相关文章
|
3月前
|
Dart IDE Unix
CMake 教程3
CMake 教程
38 1
|
3月前
CMake 教程2
CMake 教程
24 0
|
5月前
|
C++ 存储 测试技术
面向 C++ 的现代 CMake 教程(五)(3)
面向 C++ 的现代 CMake 教程(五)
61 0
|
5月前
|
C++ Unix Windows
面向 C++ 的现代 CMake 教程(四)(3)
面向 C++ 的现代 CMake 教程(四)
50 0
|
5月前
|
C++ 存储 索引
面向 C++ 的现代 CMake 教程(一)(5)
面向 C++ 的现代 CMake 教程(一)
95 0
|
5月前
|
C++ 测试技术 开发工具
面向 C++ 的现代 CMake 教程(五)(1)
面向 C++ 的现代 CMake 教程(五)
54 0
|
5月前
|
存储 算法 C++
面向 C++ 的现代 CMake 教程(五)(5)
面向 C++ 的现代 CMake 教程(五)
47 0
|
5月前
|
缓存 存储 C++
面向 C++ 的现代 CMake 教程(一)(4)
面向 C++ 的现代 CMake 教程(一)
72 0
|
5月前
|
C++ 开发者 缓存
面向 C++ 的现代 CMake 教程(四)(1)
面向 C++ 的现代 CMake 教程(四)
62 0
|
5月前
|
缓存 C++ Windows
面向 C++ 的现代 CMake 教程(一)(2)
面向 C++ 的现代 CMake 教程(一)
135 0