CMake 教程2

简介: CMake 教程

CMake 教程1:https://developer.aliyun.com/article/1597124

math

math — CMake 3.24.0-rc3 Documentation

# 评估一个数学表达式
math(EXPR <variable> "<expression>" [OUTPUT_FORMAT <format>])
 
# 示例
math(EXPR value "100 * 0xA" OUTPUT_FORMAT DECIMAL)      # value is set to "1000"
math(EXPR value "100 * 0xA" OUTPUT_FORMAT HEXADECIMAL)  # value is set to "0x3e8"

message

message — CMake 3.24.0-rc3 Documentation

# 一般信息  General messages  
  message([<mode>] "message text" ...)
 
# 报告检查  Reporting checks
  message(<checkState> "message text" ...)
 
# 可选项 mode
# FATAL_ERROR  
#     CMake 错误,停止处理和生成
# SEND_ERROR  
#     CMake 错误,继续处理,但跳过生成
# WARNING 
#     CMake 警告,继续处理
# AUTHOR_WARNING
#     CMake 警告(开发),继续处理
# DEPRECATION
#     CMake 弃用错误或警告
# (无)或NOTICE
#     打印到 stderr 以吸引用户注意的重要消息
# STATUS
#     项目用户可能感兴趣的主要有趣信息
# VERBOSE
#     针对项目用户的详细信息消息
# DEBUG
#     详细的信息消息旨在为项目本身的开发人员而不是只想构建它的用户提供
# TRACE
#     具有非常低级实现细节的细粒度消息

option

option — CMake 3.24.0-rc3 Documentation

# 提供布尔选项给用户选择
option(<variable> "<help_text>" [value])
 
# 如果<value>没有提供初始值,则布尔值默认值是OFF。如果<variable>通过普通或缓存变量进行了设置,则该命令不执行任何操作

return

return — CMake 3.24.0-rc3 Documentation

return()

separate_arguments

separate_arguments — CMake 3.24.0-rc3 Documentation

# 将命令行参数解析为分号分隔的列表
# 将空格分隔的字符串解析<args>为项目列表,并将此列表以分号分隔的标准格式存储在<variable>
separate_arguments(<variable> <mode> [PROGRAM [SEPARATE_ARGS]] <args>)
 
# mode 可取 UNIX_COMMAND, WINDOWS_COMMAND, NATIVE_COMMAND
 
 
 
 
# 示例
separate_arguments (out UNIX_COMMAND PROGRAM "cc -c main.c")
# out值 /path/to/cc;-c main.c
 
separate_arguments (out UNIX_COMMAND PROGRAM SEPARATE_ARGS "cc -c main.c")
# out值 /path/to/cc;-c;main.c

set

set — CMake 3.24.0-rc3 Documentation

Cmake命令之set介绍 - 百度文库

cmake命令之set_物随心转的博客-CSDN博客_cmake set命令

# 设置普通变量  Set Normal Variable
set(<variable> <value>... [PARENT_SCOPE])
 
 
# 设置缓存条目  Set Cache Entry
set(<variable> <value>... CACHE <type> <docstring> [FORCE])
 
# <type>
# BOOL
#    布尔ON/OFF值。 cmake-gui(1)提供一个复选框。
# FILEPATH
#    磁盘上文件的路径。 cmake-gui(1)提供文件对话框。
# PATH
#    磁盘上目录的路径。 cmake-gui(1)提供文件对话框。
# STRING
#    一行文字。 cmake-gui(1)如果STRINGS缓存条目属性已设置。
# INTERNAL
#    一行文字。 cmake-gui(1)不显示内部条目。它们可用于跨运行持久存储变量。使用这种类型意味着FORCE.
 
 
# 设置环境变量  Set Environment Variable
set(ENV{<variable>} [<value>])
 
# 使用 $ENV{<variable>} 引用此环境变量

set_directory_properties

set_directory_properties — CMake 3.24.0-rc3 Documentation

1. # 设置当

set_property

set_property — CMake 3.24.0-rc3 Documentation

# 在给定范围内设置命名属性
set_property(<GLOBAL                      |
              DIRECTORY [<dir>]           |
              TARGET    [<target1> ...]   |
              SOURCE    [<src1> ...]
                        [DIRECTORY <dirs> ...]
                        [TARGET_DIRECTORY <targets> ...] |
              INSTALL   [<file1> ...]     |
              TEST      [<test1> ...]     |
              CACHE     [<entry1> ...]    >
             [APPEND] [APPEND_STRING]
             PROPERTY <name> [<value1> ...])

site_name

site_name — CMake 3.24.0-rc3 Documentation

# 将给定变量设置为计算机的名称
site_name(variable)

string

string — CMake 3.24.0-rc3 Documentation

# 字符串操作
# 搜索和替换  Search and Replace
  string(FIND <string> <substring> <out-var> [...])
  string(REPLACE <match-string> <replace-string> <out-var> <input>...)
  string(REGEX MATCH <match-regex> <out-var> <input>...)
  string(REGEX MATCHALL <match-regex> <out-var> <input>...)
  string(REGEX REPLACE <match-regex> <replace-expr> <out-var> <input>...)
 
# 操作  Manipulation
  string(APPEND <string-var> [<input>...])
  string(PREPEND <string-var> [<input>...])
  string(CONCAT <out-var> [<input>...])
  string(JOIN <glue> <out-var> [<input>...])
  string(TOLOWER <string> <out-var>)
  string(TOUPPER <string> <out-var>)
  string(LENGTH <string> <out-var>)
  string(SUBSTRING <string> <begin> <length> <out-var>)
  string(STRIP <string> <out-var>)
  string(GENEX_STRIP <string> <out-var>)
  string(REPEAT <string> <count> <out-var>)
 
# 比较  Comparison
  string(COMPARE <op> <string1> <string2> <out-var>)
 
# 哈希  Hashing
  string(<HASH> <out-var> <input>)
 
# 生成  Generation
  string(ASCII <number>... <out-var>)
  string(HEX <string> <out-var>)
  string(CONFIGURE <string> <out-var> [...])
  string(MAKE_C_IDENTIFIER <string> <out-var>)
  string(RANDOM [<option>...] <out-var>)
  string(TIMESTAMP <out-var> [<format string>] [UTC])
  string(UUID <out-var> ...)
 
# JSON
  string(JSON <out-var> [ERROR_VARIABLE <error-var>]
         {GET | TYPE | LENGTH | REMOVE}
         <json-string> <member|index> [<member|index> ...])
  string(JSON <out-var> [ERROR_VARIABLE <error-var>]
         MEMBER <json-string>
         [<member|index> ...] <index>)
  string(JSON <out-var> [ERROR_VARIABLE <error-var>]
         SET <json-string>
         <member|index> [<member|index> ...] <value>)
  string(JSON <out-var> [ERROR_VARIABLE <error-var>]
         EQUAL <json-string1> <json-string2>)

unset

unset — CMake 3.24.0-rc3 Documentation

# 取消设置普通变量或缓存条目
# Unset Normal Variable or Cache Entry
unset(<variable> [CACHE | PARENT_SCOPE])
 
 
# 取消设置环境变量
# Unset Environment Variable
unset(ENV{<variable>})

variable_watch

variable_watch — CMake 3.24.0-rc3 Documentation

# 观察 CMake 变量的变化
variable_watch(<variable> [<command>])
 
# 如果指定的<variable>更改并且没有<command>给出,将打印一条消息以通知更改。
# 如果<command>给出,则将执行此命令。该命令将接收以下参数: COMMAND(<variable> <access> <value> <current_list_file> <stack>)

while

while — CMake 3.24.0-rc3 Documentation

while(<condition>)
  <commands>
endwhile()

项目命令(Project Commands)

add_compile_definitions

add_compile_definitions — CMake 3.24.0-rc3 Documentation

cmake(13):构建时设置预处理宏定义以及add_compile_definitions命令详解_翔底的博客-CSDN博客_cmake预定义宏

CMAKE语法:add_definitions、add_compile_options_持续学习,不断沉淀的博客-CSDN博客_add_compile_options

cmake的四个命令:add_compile_options、add_definitions、target_compile_definitions、build_command - DoubleLi - 博客园

# 将预处理器定义添加到源文件的编译中
# 为当前路径以及下层路径的目标加入编译定义
add_compile_definitions(<definition> ...)
 
# 示例 
# add_definitions 推荐使用 add_compile_definitions 替代
add_definitions (-DCOMPILE_M1)
add_definitions (-DCOMPILE_M2=0x100)
 
add_compile_definitions (COMPILE_M3=0x200)
 
if(COMPILE_M4)
    add_compile_definitions (COMPILE_M4)
endif()

add_compile_options

add_compile_options — CMake 3.24.0-rc3 Documentation

# 为源文件的添加编译选项
add_compile_options(<option> ...)
 
# 示例
if (MSVC)
    # warning level 4 and all warnings as errors
    add_compile_options(/W4 /WX)
else()
    # lots of warnings and all warnings as errors
    add_compile_options(-Wall -Wextra -pedantic -Werror)
endif()

add_custom_command

add_custom_command — CMake 3.24.0-rc3 Documentation

# 将自定义构建规则添加到生成的构建系统
# 生成文件
add_custom_command(OUTPUT output1 [output2 ...]
                   COMMAND command1 [ARGS] [args1...]
                   [COMMAND command2 [ARGS] [args2...] ...]
                   [MAIN_DEPENDENCY depend]
                   [DEPENDS [depends...]]
                   [BYPRODUCTS [files...]]
                   [IMPLICIT_DEPENDS <lang1> depend1
                                    [<lang2> depend2] ...]
                   [WORKING_DIRECTORY dir]
                   [COMMENT comment]
                   [DEPFILE depfile]
                   [JOB_POOL job_pool]
                   [VERBATIM] [APPEND] [USES_TERMINAL]
                   [COMMAND_EXPAND_LISTS])
 
# 示例:生成文件
# 自定义命令可用于生成源文件。例如,代码:
 
add_custom_command(
  OUTPUT out.c
  COMMAND someTool -i ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
                   -o out.c
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/in.txt
  VERBATIM)
add_library(myLib out.c)
 
 
 
# 构建事件
# 第二个签名将自定义命令添加到库或可执行文件等目标。这对于在构建目标之前或之后执行操作很有用。该命令成为目标的一部分,并且只会在构建目标本身时执行。如果目标已经构建,该命令将不会执行。
 
add_custom_command(TARGET <target>
                   PRE_BUILD | PRE_LINK | POST_BUILD
                   COMMAND command1 [ARGS] [args1...]
                   [COMMAND command2 [ARGS] [args2...] ...]
                   [BYPRODUCTS [files...]]
                   [WORKING_DIRECTORY dir]
                   [COMMENT comment]
                   [VERBATIM] [USES_TERMINAL]
                   [COMMAND_EXPAND_LISTS])
 
# 示例:构建事件
# 一个POST_BUILD事件可用于在链接后对二进制文件进行后处理。例如,代码:
 
add_executable(myExe myExe.c)
add_custom_command(
  TARGET myExe POST_BUILD
  COMMAND someHasher -i "$<TARGET_FILE:myExe>"
                     -o "$<TARGET_FILE:myExe>.hash"
  VERBATIM)

add_custom_target

add_custom_target — CMake 3.24.0-rc3 Documentation

# 添加一个没有输出的目标,以便始终构建它
add_custom_target(Name [ALL] [command1 [args1...]]
                  [COMMAND command2 [args2...] ...]
                  [DEPENDS depend depend depend ... ]
                  [BYPRODUCTS [files...]]
                  [WORKING_DIRECTORY dir]
                  [COMMENT comment]
                  [JOB_POOL job_pool]
                  [VERBATIM] [USES_TERMINAL]
                  [COMMAND_EXPAND_LISTS]
                  [SOURCES src1 [src2...]])

add_definitions

add_definitions — CMake 3.24.0-rc3 Documentation

# 将 -D 定义标志添加到源文件的编译中
add_definitions(-DFOO -DBAR ...)
 
# 注意 此命令已被替代命令取代:
#    利用add_compile_definitions()添加预处理器定义。
#    利用include_directories()添加包含目录。
#    利用add_compile_options()添加其他选项

add_dependencies

add_dependencies — CMake 3.24.0-rc3 Documentation

# 在顶级目标之间添加依赖关系。
 
add_dependencies(<target> [<target-dependency>]...)

add_executable

add_executable — CMake 3.24.0-rc3 Documentation

# 普通的可执行文件
# Normal Executables
add_executable(<name> [WIN32] [MACOSX_BUNDLE]
               [EXCLUDE_FROM_ALL]
               [source1] [source2 ...])
 
 
# 导入的可执行文件
# Imported Executables
add_executable(<name> IMPORTED [GLOBAL])
 
 
# 给可执行文件创建一个别名
# Alias Executables
add_executable(<name> ALIAS <target>)
 

add_library

add_library — CMake 3.24.0-rc3 Documentation

https://www.csdn.net/tags/NtzaQgzsOTQ3MTAtYmxvZwO0O0OO0O0O.html

cmake : add_library详解_lainegates的博客-CSDN博客_add_library

# 普通库
# Normal Libraries
add_library(<name> [STATIC | SHARED | MODULE]
            [EXCLUDE_FROM_ALL]
            [<source>...])
 
 
# 对象库
# Object Libraries
add_library(<name> OBJECT [<source>...])
 
 
# 接口库
# Interface Libraries
add_library(<name> INTERFACE)
 
 
# 导入的库
# Imported Libraries
add_library(<name> <type> IMPORTED [GLOBAL])
 
 
# 别名库
# Alias Libraries
add_library(<name> ALIAS <target>)

add_link_options

add_link_options — CMake 3.24.0-rc3 Documentation

# 为当前目录及以下目录中的可执行文件、共享库或模块库目标的链接步骤添加选项,这些选项是在调用此命令后添加的
add_link_options(<option> ...)

add_subdirectory

add_subdirectory — CMake 3.24.0-rc3 Documentation

# 将子目录添加到构建。
 
add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])

add_test

add_test — CMake 3.24.0-rc3 Documentation

add_test(NAME <name> COMMAND <command> [<arg>...]
         [CONFIGURATIONS <config>...]
         [WORKING_DIRECTORY <dir>]
         [COMMAND_EXPAND_LISTS])

aux_source_directory

aux_source_directory — CMake 3.24.0-rc3 Documentation

# 查找目录中的所有源文件。
 
aux_source_directory(<dir> <variable>)

build_command

create_test_sourcelist

define_property

enable_language

enable_language — CMake 3.24.0-rc3 Documentation

# 启用一种语言(CXX/C/OBJC/OBJCXX/Fortran/等)
 
enable_language(<lang> [OPTIONAL] )

enable_testing

enable_testing — CMake 3.24.0-rc3 Documentation

# 启用当前目录及以下目录的测试。
 
enable_testing()


export

fltk_wrap_ui

get_source_file_property

get_target_property

get_test_property

include_directories

include_directories — CMake 3.24.0-rc3 Documentation

# 为项目构建添加头文件搜索目录
 
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])
 
# 搜索目录添加到属性  INCLUDE_DIRECTORIES 中

include_external_msproject

include_regular_expression

install

install — CMake 3.24.0-rc3 Documentation

install(TARGETS <target>... [...])
install(IMPORTED_RUNTIME_ARTIFACTS <target>... [...])
install({FILES | PROGRAMS} <file>... [...])
install(DIRECTORY <dir>... [...])
install(SCRIPT <file> [...])
install(CODE <code> [...])
install(EXPORT <export-name> [...])
install(RUNTIME_DEPENDENCY_SET <set-name> [...])
 
 
# 示例
install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)
 
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  DESTINATION include
  )

link_directories

link_directories — CMake 3.24.0-rc3 Documentation

# 添加目录到链接器搜索库的目录中。
 
link_directories([AFTER|BEFORE] directory1 [directory2 ...])
 
目录会设置到 LINK_DIRECTORIES 属性中

link_libraries

link_libraries — CMake 3.24.0-rc3 Documentation

target_link_libraries 和link_libraries区别_日作诗歌三千首的博客-CSDN博客_target_link_libraries

# 将库链接到以后添加的所有目标。
 
link_libraries([item1 [item2 [...]]]
               [[debug|optimized|general] <item>] ...)

load_cache

project

project — CMake 3.24.0-rc3 Documentation

# 设置项目的名称
project(<PROJECT-NAME> [<language-name>...])
project(<PROJECT-NAME>
        [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
        [DESCRIPTION <project-description-string>]
        [HOMEPAGE_URL <url-string>]
        [LANGUAGES <language-name>...])
 
 
# 示例
project(nlohmann_json VERSION 3.10.5 LANGUAGES CXX)

remove_definitions

remove_definitions — CMake 3.24.0-rc3 Documentation

# 删除由 -D 添加的变量
 
remove_definitions(-DFOO -DBAR ...)
 
# 见 add_definitions()


set_source_files_properties

set_target_properties

set_tests_properties

source_group

target_compile_definitions

target_compile_definitions — CMake 3.24.0-rc3 Documentation

target_compile_definitions(<target>
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
 
# PRIVATE 和 PUBLIC items 会设置到<target>的 COMPILE_DEFINITIONS 属性中. 
# PUBLIC 和 INTERFACE items 会设置到<target>的 INTERFACE_COMPILE_DEFINITIONS 属性中.
 
 
# 项目上的任何前导-D都将被删除。空项目被忽略。例如,以下都是等价的:
target_compile_definitions(foo PUBLIC FOO)
target_compile_definitions(foo PUBLIC -DFOO)  # -D removed
target_compile_definitions(foo PUBLIC "" FOO) # "" ignored
target_compile_definitions(foo PUBLIC -D FOO) # -D becomes "", then ignored
 
# 定义可选择的值:
target_compile_definitions(foo PUBLIC FOO=1)

target_compile_features

target_compile_features — CMake 3.24.0-rc3 Documentation

# 将预期的编译器功能添加到目标。
 
target_compile_features(<target> <PRIVATE|PUBLIC|INTERFACE> <feature> [...])

target_compile_options

target_compile_options — CMake 3.24.0-rc3 Documentation

# 将编译选项添加到目标。
 
target_compile_options(<target> [BEFORE]
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

target_include_directories

target_include_directories — CMake 3.24.0-rc3 Documentation

# 将包含目录添加到目标
target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
 
 
# PRIVATE 和 PUBLIC items 会设置到<target>的 INCLUDE_DIRECTORIES 属性中. 
# PUBLIC 和 INTERFACE items 会设置到<target>的 INTERFACE_INCLUDE_DIRECTORIES属性中.

target_link_directories

target_link_directories — CMake 3.24.0-rc3 Documentation

# 将链接目录添加到目标。
 
target_link_directories(<target> [BEFORE]
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
 
 
# PRIVATE 和 PUBLIC items 会设置到<target>的 LINK_DIRECTORIES 属性中. 
# PUBLIC 和 INTERFACE items 会设置到<target>的  INTERFACE_LINK_DIRECTORIES 属性中.

target_link_libraries

target_link_libraries — CMake 3.24.0-rc3 Documentation

# 为<target>指定其要依赖项的库或标志
target_link_libraries(<target> ... <item>... ...)
 
 
# 目标和/或其依赖的库
target_link_libraries(<target>
                      <PRIVATE|PUBLIC|INTERFACE> <item>...
                     [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
 
 
# 目标及其依赖的库
target_link_libraries(<target> <item>...)

CMake 教程3:https://developer.aliyun.com/article/1597131?spm=a2c6h.13148508.setting.18.51734f0e1J7OoF

目录
相关文章
|
24天前
|
Dart IDE Unix
CMake 教程3
CMake 教程
20 1
|
24天前
|
缓存 Unix 程序员
CMake 教程1
CMake 教程
49 1
|
3月前
|
缓存 存储 C++
面向 C++ 的现代 CMake 教程(一)(4)
面向 C++ 的现代 CMake 教程(一)
65 0
|
3月前
|
存储 算法 C++
面向 C++ 的现代 CMake 教程(五)(5)
面向 C++ 的现代 CMake 教程(五)
36 0
|
3月前
|
C++ 存储 测试技术
面向 C++ 的现代 CMake 教程(五)(2)
面向 C++ 的现代 CMake 教程(五)
55 0
|
3月前
|
C++ 容器 Docker
面向 C++ 的现代 CMake 教程(一)(1)
面向 C++ 的现代 CMake 教程(一)
100 0
|
3月前
|
缓存 C++ Windows
面向 C++ 的现代 CMake 教程(一)(2)
面向 C++ 的现代 CMake 教程(一)
91 0
|
3月前
|
C++ 存储 JSON
面向 C++ 的现代 CMake 教程(五)(4)
面向 C++ 的现代 CMake 教程(五)
55 0
|
3月前
|
C++ 存储 索引
面向 C++ 的现代 CMake 教程(一)(5)
面向 C++ 的现代 CMake 教程(一)
64 0
|
3月前
|
C++ 测试技术 开发工具
面向 C++ 的现代 CMake 教程(五)(1)
面向 C++ 的现代 CMake 教程(五)
42 0