CMake教程-第 4 步:添加生成器表达式

CMake教程-第 4 步:添加生成器表达式

  • 1 CMake教程介绍
  • 2 学习步骤
    • Step 1: A Basic Starting Point
    • Step 2: Adding a Library
    • Step 3: Adding Usage Requirements for a Library
    • Step 4: Adding Generator Expressions
    • Step 5: Installing and Testing
    • Step 6: Adding Support for a Testing Dashboard
    • Step 7: Adding System Introspection
    • Step 8: Adding a Custom Command and Generated File
    • Step 9: Packaging an Installer
    • Step 10: Selecting Static or Shared Libraries
    • Step 11: Adding Export Configuration
    • Step 12: Packaging Debug and Release
  • 3 Step 4: Adding Generator Expressions
    • 3.1 Exercise 1 - Adding Compiler Warning Flags with Generator Expressions
      • 3.1.1 目标
      • 3.1.2 Helpful Resources(有用的资源)
      • 3.1.3 Files to Edit(需编辑的文件)
      • 3.1.4 Getting Started(入门指南)
      • 3.1.5 Build and Run(构建并运行)
      • 3.1.6 解决方案
      • 3.1.7 CMakeLists.txt
      • 3.1.8 运行结果

该文档是基于CMake的官方教程翻译而来,并稍微添加了自己的理解:

cmake的官方网站为:CMake Tutorial

1 CMake教程介绍

The CMake tutorial provides a step-by-step guide that covers common build system issues that CMake helps address. Seeing how various topics all work together in an example project can be very helpful.
CMake 教程提供了一个循序渐进的指南,涵盖了 CMake 可帮助解决的常见构建系统问题。在一个示例项目中了解各个主题是如何协同工作的,会非常有帮助。

2 学习步骤

The tutorial source code examples are available in this archive. Each step has its own subdirectory containing code that may be used as a starting point. The tutorial examples are progressive so that each step provides the complete solution for the previous step.
本文档中提供了教程源代码示例。每个步骤都有自己的子目录,其中包含可用作起点的代码。教程示例是循序渐进的,因此每一步都提供了前一步的完整解决方案。

Step 1: A Basic Starting Point

  • Exercise 1 - Building a Basic Project
  • Exercise 2 - Specifying the C++ Standard
  • Exercise 3 - Adding a Version Number and Configured Header File

Step 2: Adding a Library

  • Exercise 1 - Creating a Library
  • Exercise 2 - Adding an Option

Step 3: Adding Usage Requirements for a Library

  • Exercise 1 - Adding Usage Requirements for a Library
  • Exercise 2 - Setting the C++ Standard with Interface Libraries

Step 4: Adding Generator Expressions

  • Exercise 1 - Adding Compiler Warning Flags with Generator Expressions

Step 5: Installing and Testing

  • Exercise 1 - Install Rules
  • Exercise 2 - Testing Support

Step 6: Adding Support for a Testing Dashboard

  • Exercise 1 - Send Results to a Testing Dashboard

Step 7: Adding System Introspection

  • Exercise 1 - Assessing Dependency Availability

Step 8: Adding a Custom Command and Generated File

Step 9: Packaging an Installer

Step 10: Selecting Static or Shared Libraries

Step 11: Adding Export Configuration

Step 12: Packaging Debug and Release

3 Step 4: Adding Generator Expressions

Generator expressions are evaluated during build system generation to produce information specific to each build configuration.
生成器表达式在构建系统生成期间进行评估,以生成特定于每个构建配置的信息。

Generator expressions are allowed in the context of many target properties, such as LINK_LIBRARIES, INCLUDE_DIRECTORIES, COMPILE_DEFINITIONS and others. They may also be used when using commands to populate those properties, such as target_link_libraries(), target_include_directories(), target_compile_definitions() and others.
在多目标属性(如 LINK_LIBRARIESINCLUDE_DIRECTORIESCOMPILE_DEFINITIONS 等)的上下文中都允许使用生成器表达式。在使用 target_link_libraries()target_include_directories()target_compile_definitions() 等命令填充这些属性时,也可以使用这些表达式。

Generator expressions may be used to enable conditional linking, conditional definitions used when compiling, conditional include directories and more. The conditions may be based on the build configuration, target properties, platform information or any other queryable information.
生成器表达式可用于启用条件链接、编译时使用的条件定义、条件包含目录等。这些条件可以基于编译配置、目标属性、平台信息或任何其他可查询的信息。

There are different types of generator expressions including Logical, Informational, and Output expressions.
生成器表达式有多种类型,包括逻辑表达式、信息表达式和输出表达式。

Logical expressions are used to create conditional output. The basic expressions are the 0 and 1 expressions. A < 0 : . . . > r e s u l t s i n t h e e m p t y s t r i n g , a n d < 1 : . . . > r e s u l t s i n t h e c o n t e n t o f . . . . T h e y c a n a l s o b e n e s t e d . 逻辑表达式用于创建条件输出。基本表达式是 0 和 1 表达式。 <0:...> results in the empty string, and <1:...> results in the content of .... They can also be nested. 逻辑表达式用于创建条件输出。基本表达式是 0 和 1 表达式。 <0:...>resultsintheemptystring,and<1:...>resultsinthecontentof....Theycanalsobenested.逻辑表达式用于创建条件输出。基本表达式是01表达式。<0:…>的结果是空字符串,<1:…>的结果是 … 的内容。它们还可以嵌套。

3.1 Exercise 1 - Adding Compiler Warning Flags with Generator Expressions

A common usage of generator expressions is to conditionally add compiler flags, such as those for language levels or warnings. A nice pattern is to associate this information to an INTERFACE target allowing this information to propagate.
生成器表达式的一个常见用法是有条件地添加编译器标志,如语言级别或警告标志。一种不错的模式是将这些信息与 INTERFACE 目标相关联,从而使这些信息得以传播。

3.1.1 目标

Add compiler warning flags when building but not for installed versions.
编译时添加编译器警告标志,但不针对已安装的版本。

3.1.2 Helpful Resources(有用的资源)

  • cmake-generator-expressions(7)
  • cmake_minimum_required()
  • set()
  • target_compile_options()

3.1.3 Files to Edit(需编辑的文件)

  • CMakeLists.txt

3.1.4 Getting Started(入门指南)

Open the file Step4/CMakeLists.txt and complete TODO 1 through TODO 4.
打开文件 Step4/CMakeLists.txt,并完成 TODO 1 至 TODO 4。

First, in the top level CMakeLists.txt file, we need to set the cmake_minimum_required() to 3.15. In this exercise we are going to use a generator expression which was introduced in CMake 3.15.
首先,在顶层 CMakeLists.txt 文件中,我们需要将 cmake_minimum_required() 设置为 3.15。在本练习中,我们将使用 CMake 3.15 中引入的生成器表达式。

Next we add the desired compiler warning flags that we want for our project. As warning flags vary based on the compiler, we use the COMPILE_LANG_AND_ID generator expression to control which flags to apply given a language and a set of compiler ids.
接下来,我们为项目添加所需的编译器警告标志。由于警告标志因编译器而异,我们使用 COMPILE_LANG_AND_ID 生成器表达式来控制在给定语言和编译器 ID 的情况下应用哪些标志。

3.1.5 Build and Run(构建并运行)

Make a new directory called Step4_build, run the cmake executable or the cmake-gui to configure the project and then build it with your chosen build tool or by using cmake --build . from the build directory.
创建一个名为 Step4_build 的目录,运行 cmake 可执行文件或 cmake-gui 配置项目,然后使用你选择的构建工具或从构建目录中使用 cmake --build .构建项目。

mkdir Step4_build
cd Step4_build
cmake ../Step4
cmake --build .

3.1.6 解决方案

Update the cmake_minimum_required() to require at least CMake version 3.15:
更新 cmake_minimum_required() 以至少需要 CMake 3.15版本 :

TODO 1: Click to show/hide answer

TODO 1: CMakeLists.txt
cmake_minimum_required(VERSION 3.15)

Next we determine which compiler our system is currently using to build since warning flags vary based on the compiler we use. This is done with the COMPILE_LANG_AND_ID generator expression. We set the result in the variables gcc_like_cxx and msvc_cxx as follows:
接下来,我们要确定系统当前使用的编译器,因为警告标志会根据编译器的不同而变化。这是通过 COMPILE_LANG_AND_ID 生成器表达式完成的。我们在变量 gcc_like_cxxmsvc_cxx 中设置结果如下:

TODO 2: Click to show/hide answer

TODO 2: CMakeLists.txt
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")

Next we add the desired compiler warning flags that we want for our project. Using our variables gcc_like_cxx and msvc_cxx, we can use another generator expression to apply the respective flags only when the variables are true. We use target_compile_options() to apply these flags to our interface library.
接下来,我们为项目添加所需的编译器警告标志。使用我们的变量 gcc_like_cxxmsvc_cxx,我们可以使用另一个生成器表达式,只有当变量为 true 时才应用相应的标记。我们使用 target_compile_options() 将这些标志应用到我们的接口库中。

TODO 3: Click to show/hide answer

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

Lastly, we only want these warning flags to be used during builds. Consumers of our installed project should not inherit our warning flags. To specify this, we wrap our flags in a generator expression using the BUILD_INTERFACE condition. The resulting full code looks like the following:
最后,我们只希望在构建过程中使用这些警告标志。已安装项目的用户不应继承我们的警告标志。为此,我们使用 BUILD_INTERFACE 条件在生成器表达式中封装了警告标志。生成的完整代码如下

TODO 4: Click to show/hide answer

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

3.1.7 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
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()
# 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                                                                                                                                                                                                                                                     
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}")

3.1.8 运行结果

执行Tutorial程序的运行结果如下所示:

$ ./Tutorial 25
Computing sqrt of 25 to be 13
Computing sqrt of 25 to be 7.46154
Computing sqrt of 25 to be 5.40603
Computing sqrt of 25 to be 5.01525
Computing sqrt of 25 to be 5.00002
Computing sqrt of 25 to be 5
Computing sqrt of 25 to be 5
Computing sqrt of 25 to be 5
Computing sqrt of 25 to be 5
Computing sqrt of 25 to be 5
The square root of 25 is 5
$ 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/145180.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

第5讲:v-if与v-show的使用方法及区别

v-if条件判断 v-if是条件渲染指令&#xff0c;它根据表达式的真假来删除和插入元素&#xff0c;它的基本语法如下&#xff1a; v-if “expression” expression是一个返回bool值的表达式&#xff0c;表达式可以是一个bool属性&#xff0c;也可以是一个返回bool的运算式 &#…

RTSP协议抓包及讲解

文章目录 前言一、RTSP 亲手搭建直播点播1、数据源为视频文件2、数据源为摄像头①、搭建 RTSP 流媒体服务器②、客户端拉流 二、RTSP 协议简介三、手撕 RTSP 协议1、Wireshark 抓包①、搭建环境②、wireshark 抓包 2、RTSP 交互流程①、OPTIONS②、DESCRIBE③、SETUP④、PLAY⑤…

buildroot移植Qt5无法显示字体问题

报错&#xff1a;QFontDatabase: Cannot find font directory /usr/lib/fonts. Note that Qt no longer ships fonts. Deploy some (from DejaVu Fonts for example) or switch to fontconfig. 原因&#xff1a;很明显是Qt没有找到字库文件&#xff1b; 解决方法&#xff1a; 1…

[old]TeamDev DotNetBrowser Crack

TeamDev DotNetBrowser将 Chromium Web 浏览器添加到您的 .NET 应用程序中。在 WPF 和 WinForms 中显示现代网页。使用 DOM、JS、网络、打印等。在 Windows x86/x64/ARM64、macOS x64/Apple Silicon、Linux x64/ARM64 上运行&#xff0c;支持.NET Framework 4.5 特征 HTML5、C…

神经辐射场(NeRF)2023最新论文及源代码合集

神经辐射场&#xff08;NeRF&#xff09;作为一种先进的计算机图形学技术&#xff0c;能够生成高质量的三维重建模型&#xff0c;在计算机图形学、计算机视觉、增强现实等领域都有着广泛的应用前景&#xff0c;因此&#xff0c;自2020年惊艳亮相后&#xff0c;神经辐射场也成为…

C/C++指针笔试题详解

个人主页&#xff1a;点我进入主页 专栏分类&#xff1a;C语言初阶 C语言程序设计————KTV C语言小游戏 C语言进阶 C语言刷题 欢迎大家点赞&#xff0c;评论&#xff0c;收藏。 一起努力&#xff0c;一起奔赴大厂。 目录 1.前言 2.指针题写出下列程序的结…

十六.镜头知识之工业镜头的质量判断因素

十六.镜头知识之工业镜头的质量判断因素 文章目录 十六.镜头知识之工业镜头的质量判断因素1.分辨率(Resolution)2.明锐度(Acutance)3.景深(DOF)&#xff1a;4. 最大相对孔径与光圈系数5.工业镜头各参数间的相互影响关系5.1.焦距大小的影响情况5.2.光圈大小的影响情况5.3.像场中…

混淆技术研究-混淆技术-源码分析(2)

简介 OLLVM(Obfuscator-LLVM)是基于LLVM(Low Level Virtual Machine)框架的一种代码混淆器。它主要用于对C/C++和汇编语言程序进行混淆,以增加代码的复杂性,提高代码的安全性和抵抗逆向工程的能力。 IR(Intermediate Representation)是指中间表示,是编译器在将源代码…

【7.Vue 利用Heatmap.js 制作自定义热力图】

1.效果 2.背景 需要根据后端检测的设备的数值显示设备周围的空气情况&#xff0c;用户希望用热力图的方式来显示&#xff0c;于是在网上找了资料&#xff0c;发现可以用Heatmap.js来实现。 Heatmap.js 官网&#xff1a;https://www.patrick-wied.at/static/heatmapjs/ 3.引入…

基于微信小程序的宠物寄养平台小程序设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言系统主要功能&#xff1a;具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09;有保障的售后福利 代码参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计…

Docker的学习记录

Docker是一个被广泛使用的开源容器引擎&#xff0c;基于Go语言&#xff0c;遵从Apache2.0协议开源。 docker的三个概念&#xff1a;容器、镜像和仓库。 镜像&#xff08;Image&#xff09;&#xff1a;镜像是Docker中的一个模板。通过 Docker镜像 来创建 Docker容器&#xff…

(一)TinyWebServer的环境配置与运行

Linux下C轻量级Web服务器&#xff0c;项目来源于&#xff1a;TinyWebServer 配置环境&#xff08;为下载代码&#xff0c;编译运行做准备&#xff09; 1. 安装VMware VMware官网 选择产品&#xff0c;点击Workstation Pro 下载试用版&#xff08;注&#xff1a;需要在官网注册…

泡泡玛特城市乐园即将开园 解锁“文化+科技”潮流空间

近年来&#xff0c;泡泡玛特以潮玩IP为核心&#xff0c;不断拓展业务版图&#xff0c;推进国际化布局同时实现集团化运营&#xff0c;而泡泡玛特首个城市乐园即将开业。 据了解&#xff0c;泡泡玛特城市乐园是由泡泡玛特精心打造的沉浸式IP主题乐园&#xff0c;占地约4万平方米…

7.2 怎样定义函数

7.2.1 为什么要定义函数 主要内容&#xff1a; 为什么要定义函数 C语言要求所有在程序中用到的函数必须“先定义&#xff0c;后使用”。这是因为在调用一个函数之前&#xff0c;编译系统需要知道这个函数的名字、返回值类型、功能以及参数的个数与类型。如果没有事先定义&…

第三章 图标辅助元素的定制

第三章 图标辅助元素的定制 1.认识图表常用的辅助元素 ​ 图表的辅助元素是指除了根据数据绘制的图形之外的元素&#xff0c;常用的辅助元素包括坐标轴、标题、图例、网格、参考线、参考区域、注释文本和表格&#xff0c;它们都可以对图形进行补充说明。 ​ 上图中图表常用辅…

上网Tips: Linux截取动态效果图工具_byzanz

链接1 链接2 安装&#xff1a; sudo apt-get install byzanz 查看指令 说明 byzanz-record --help日常操作 xwininfo点击 待录制窗口 左上角 byzanz-record -x 72 -y 64 -w 1848 -h 893 -d 10 --delay5 -c /home/xixi/myGIF/test.gif小工具 获取鼠标坐标 xdotool getm…

图像练习-答题卡opencv(02)

原图 结果 代码 // Load source imagecv::Mat src cv::imread("answer_card.jpg", cv::IMREAD_COLOR);if (src.empty()){return;}cv::Mat gray;cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);cv::Mat binary;double value cv::threshold(gray, binary, 0, 255, …

Python3操作SQLite3创建表主键自增长|CRUD基本操作

Win11查看安装的Python路径及安装的库 Python PEP8 代码规范常见问题及解决方案 Python3操作MySQL8.XX创建表|CRUD基本操作 Python3操作SQLite3创建表主键自增长|CRUD基本操作 anaconda3最新版安装|使用详情|Error: Please select a valid Python interpreter Python函数绘…

软件测试之单元测试自动化入门基础

单元测试自动化 所谓的单元测试(Unit Test)是根据特定的输入数据&#xff0c;针对程序代码中的最小实体单元的输入输出的正确性进行验证测试的过程。所谓的最小实体单元就是组织项目代码的最基本代码结构&#xff1a;函数&#xff0c;类&#xff0c;模块等。在Python中比较知名…

【数据库——MySQL】(8)表数据插入、修改和删除练习及讲解

目录 1. 题目2. 解答 1. 题目 建立的数据库 YGGL&#xff0c;向库中的 3 个表中插入多行数据记录&#xff0c;然后修改和删除一些记录。 根据下表的样本数据&#xff0c;使用 SQL 语句向 Departments 表中插入数据。 使用 SQL 语句向 Employees 表中插入前 6 条数据。 使用…