Cmake·

[#6][Adding Generator Expressions][A菌严肃的CMake手记]

美式A菌

美式A菌

229 1

Intro

复习

  • target_include_directories(x option ${CMAKE_CURRENT_SOURCE_DIR}): ?
    • option INTERFACE: ?
  • target_link_libraries(x option y): ?
    • option INTERFACE: ?

今天的目标是学习两种表达式类似

  • $<condition:true_string>
  • $<COMPILE_LANG_AND_ID:language,compiler_ids>

学习完成后我们需要拉取code然后用学习的表达式为编译器添加警告标志

开始

  • 表达式就是表达式,很简单,但是类型很多。
  • 这里就只先介绍两种
$<condition:true_string>

* 如果 `condition` 为 1,则此表达式结果为 `true_string`
* 如果 `condition` 为 0,则此表达式结果为空


$<COMPILE_LANG_AND_ID:language,compiler_ids>

* 如果当前所用的语言与 `language` 一致且编译器 ID 在 `compiler_ids` 的列表中,则表达式值为 1,否则为 0
* `language` 值主要为 `CXX` 和 `C`
* `compiler_ids` 主要有 GNU、Clang、MSVC 等,有多个时用逗号隔开

  • 根路径下的CMakeLists.txt
# TODO 1: Update the minimum required version to 3.15
cmake_minimum_required(VERSION 3.15)
# cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

# specify the C++ standard
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)

# TODO 2: Create helper variables to determine which compiler we are using:
# * Create a new variable gcc_like_cxx that is true if we are using CXX and
#   any of the following compilers: ARMClang, AppleClang, Clang, GNU, LCC
# * Create a new variable msvc_cxx that is true if we are using CXX and MSVC
# Hint: Use set() and COMPILE_LANG_AND_ID

# 这里设置两个自定义名称的变量,用于判断当前编译器是否是gcc-like或者msvc
# $<COMPILE_LANG_AND_ID:a,b,c,d,e,f,g> 是固定的写法
# 编译时,如果当前编译器是a,b,c,d,e,f,g中的任意一个,则返回true,否则返回false
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")

# TODO 3: Add warning flag compile options to the interface library
# tutorial_compiler_flags.
# * For gcc_like_cxx, add flags -Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused
# * For msvc_cxx, add flags -W3
# Hint: Use target_compile_options()

# 这里$<${}:a;b;c;d;e> 这个表达式中 对应的是 $<condition:true_string>表达式
# 如果编译器是gcc-like,则添加-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused
# 如果编译器是msvc,则添加-W3
# 如何判断编译器是gcc-like还是msvc呢?通过上面的自定义变量,在编译时会自动赋值true或者false
target_compile_options(tutorial_compiler_flags INTERFACE
  "$<${gcc_like_cxx}:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>"
  "$<${msvc_cxx}:-W3>"
)

# TODO 4: With nested generator expressions, only use the flags for the
# build-tree
# Hint: Use BUILD_INTERFACE

#  这里前面的部分依然是判断编译器是gcc-like还是msvc
# 后半部分 $<BUILD_INTERFACE:...>
# BUILD_INTERFACE 是 CMake 中的一个生成器表达式,用于区分构建树(build tree)和安装树(install tree)。
# 它通常用于在构建过程中添加特定的编译选项或包含目录,而不在安装时添加这些选项

# 构建树(Build Tree)
# 构建树是指项目在构建过程中生成的文件和目录结构。
# 它包含所有的中间文件、目标文件、生成的可执行文件和库文件等。
# 构建树通常位于构建目录中,构建目录是你运行CMake和构建工具(如Make或Ninja)的地方。

# 安装树(Install Tree)
# 安装树是指项目在安装过程中生成的文件和目录结构。
# 它包含最终安装到系统或指定安装路径中的文件,如可执行文件、库文件、头文件、配置文件等。
# 安装树通常位于安装目录中,安装目录是你指定的安装路径。

target_compile_options(tutorial_compiler_flags INTERFACE
  "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
  "$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

# add the MathFunctions library
add_subdirectory(MathFunctions)

# add the executable
add_executable(Tutorial tutorial.cxx)

target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

总结

  • 表达式呢,主要起到一个动态替换的作用。
  • A菌随手一搜呢就出现很多。
  • 所以不要纠结,用到再找哦。

所属系列

从当前文章继续阅读它所在合集中的前后内容。

相关文章

优先推荐同专题、同标签和同作者内容,补足热门文章。

评论 1

登录 后参与评论

评论 1

美式A菌
美式A菌1月3日 17:55

今天的內容真是有夠水的