CMake教程-第 8 步:添加自定义命令和生成文件
CMake教程-第 8 步:添加自定义命令和生成文件
- 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 8: Adding a Custom Command and Generated File
- 3.1 Step 8: Adding a Custom Command and Generated File
- 3.2 MathFunctions/MakeTable.cmake
- 3.3 MathFunctions/CMakeLists.txt
- 3.4 MathFunctions/mysqrt.cxx
- 3.5 编译
- 3.6 运行结果
该文档是基于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 8: Adding a Custom Command and Generated File
3.1 Step 8: Adding a Custom Command and Generated File
Suppose, for the purpose of this tutorial, we decide that we never want to use the platform log and exp functions and instead would like to generate a table of precomputed values to use in the mysqrt function. In this section, we will create the table as part of the build process, and then compile that table into our application.
假设在本教程中,我们决定不再使用平台log和 exp 函数,而是想生成一个预计算值表,供 mysqrt 函数使用。在本节中,我们将在构建过程中创建表格,然后将表格编译到应用程序中。
First, let’s remove the check for the log and exp functions in MathFunctions/CMakeLists.txt. Then remove the check for HAVE_LOG and HAVE_EXP from mysqrt.cxx. At the same time, we can remove #include .
首先,删除 MathFunctions/CMakeLists.txt
中对 log
和 exp
函数的检查。然后删除 mysqrt.cxx
中的 HAVE_LOG
和 HAVE_EXP
检查。同时,我们可以删除 #include <cmath>
。
In the MathFunctions subdirectory, a new source file named MakeTable.cxx has been provided to generate the table.
在 MathFunctions
子目录中,提供了一个名为 MakeTable.cxx
的新源文件,用于生成表格。
After reviewing the file, we can see that the table is produced as valid C++ code and that the output filename is passed in as an argument.
查看文件后,我们可以看到表格是以有效的 C++ 代码生成的,输出文件名是作为参数传递的。
The next step is to create MathFunctions/MakeTable.cmake. Then, add the appropriate commands to the file to build the MakeTable executable and then run it as part of the build process. A few commands are needed to accomplish this.
下一步是创建 MathFunctions/MakeTable.cmake。然后,在该文件中添加适当的命令来构建 MakeTable 可执行文件,并将其作为构建过程的一部分来运行。为此需要执行几条命令。
First, we add an executable for MakeTable.
首先,我们为 MakeTable 添加一个可执行程序。
MathFunctions/MakeTable.cmake
add_executable(MakeTable MakeTable.cxx)
After creating the executable, we add the tutorial_compiler_flags to our executable using target_link_libraries().
在创建可执行的,我们使用target_link_libraries()添加tutorial_compiler_flags
我们的可执行程序.
MathFunctions/MakeTable.cmake
target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags)
Then we add a custom command that specifies how to produce Table.h by running MakeTable.
然后,我们添加一条自定义命令,指定如何通过运行 MakeTable
生成 Table.h
。
MathFunctions/MakeTable.cmake
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.hCOMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.hDEPENDS MakeTable)
Next we have to let CMake know that mysqrt.cxx depends on the generated file Table.h. This is done by adding the generated Table.h to the list of sources for the library SqrtLibrary.
接下来,我们必须让 CMake
知道 mysqrt.cxx
依赖于生成的 Table.h 文件
。方法是将生成的 Table.h
添加到 SqrtLibrary
库的源代码列表中。
MathFunctions/CMakeLists.txtadd_library(SqrtLibrary STATICmysqrt.cxx${CMAKE_CURRENT_BINARY_DIR}/Table.h)
We also have to add the current binary directory to the list of include directories so that Table.h can be found and included by mysqrt.cxx.
我们还必须将当前二进制目录添加到包含目录列表中,以便 mysqrt.cxx
可以找到并包含 Table.h
。
MathFunctions/CMakeLists.txttarget_include_directories(SqrtLibrary PRIVATE${CMAKE_CURRENT_BINARY_DIR})# link SqrtLibrary to tutorial_compiler_flags
As the last step, we need to include MakeTable.cmake at the top of the MathFunctions/CMakeLists.txt
.
最后一步,我们需要在 MathFunctions/CMakeLists.txt
文件的顶部加入 MakeTable.cmake
。
MathFunctions/CMakeLists.txtinclude(MakeTable.cmake)
Now let’s use the generated table. First, modify mysqrt.cxx to include Table.h. Next, we can rewrite the mysqrt function to use the table:
现在让我们使用生成的表。首先,修改 mysqrt.cxx
以包含 Table.h
。接下来,我们可以重写 mysqrt
函数以使用表格:
MathFunctions/mysqrt.cxx
double mysqrt(double x)
{if (x <= 0) {return 0;}// use the table to help find an initial valuedouble result = x;if (x >= 1 && x < 10) {std::cout << "Use the table to help find an initial value " << std::endl;result = sqrtTable[static_cast<int>(x)];}// do ten iterationsfor (int i = 0; i < 10; ++i) {if (result <= 0) {result = 0.1;}double delta = x - (result * result);result = result + 0.5 * delta / result;std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;}return result;
}
Run the cmake executable or the cmake-gui to configure the project and then build it with your chosen build tool.
运行 cmake 可执行文件或 cmake-gui 配置项目,然后使用所选的构建工具构建项目。
When this project is built it will first build the MakeTable executable. It will then run MakeTable to produce Table.h. Finally, it will compile mysqrt.cxx which includes Table.h to produce the MathFunctions library.
当这个项目建成时,它将首先编译 MakeTable
可执行程序。然后运行 MakeTable
生成 Table.h
。最后,它将编译包含 Table.h
的 mysqrt.cxx
以生成 MathFunctions
库。
Run the Tutorial executable and verify that it is using the table.
运行 Tutorial 可执行文件并验证它是否使用了表格。
3.2 MathFunctions/MakeTable.cmake
# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)target_link_libraries(MakeTable PRIVATE tutorial_compiler_flags)# add the command to generate the source code
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.hCOMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.hDEPENDS MakeTable)
3.3 MathFunctions/CMakeLists.txt
include(MakeTable.cmake)add_library(MathFunctions MathFunctions.cxx)# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH)target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")# library that just does sqrtadd_library(SqrtLibrary STATICmysqrt.cxx${CMAKE_CURRENT_BINARY_DIR}/Table.h)target_include_directories(SqrtLibrary PRIVATE${CMAKE_CURRENT_BINARY_DIR})target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()# state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't.
target_include_directories(MathFunctionsINTERFACE ${CMAKE_CURRENT_SOURCE_DIR})# link our compiler flags interface library
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)# install libs
set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)list(APPEND installable_libs SqrtLibrary)
endif()
install(TARGETS ${installable_libs} DESTINATION lib)
# install include headers
install(FILES MathFunctions.h DESTINATION include)
3.4 MathFunctions/mysqrt.cxx
#include "mysqrt.h"
#include "Table.h"#include <iostream>namespace mathfunctions {
namespace detail {
// a hack square root calculation using simple operationsdouble mysqrt(double x)
{if (x <= 0) {return 0;}// use the table to help find an initial valuedouble result = x;if (x >= 1 && x < 10) {std::cout << "Use the table to help find an initial value " << std::endl;result = sqrtTable[static_cast<int>(x)];}// do ten iterationsfor (int i = 0; i < 10; ++i) {if (result <= 0) {result = 0.1;}double delta = x - (result * result);result = result + 0.5 * delta / result;std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;}return result;
}
}
}
3.5 编译
test@test:~/sda3/work/cmake/Step8_build$ cmake ../Step8
-- The C compiler identification is GNU 10.5.0
-- The CXX compiler identification is GNU 10.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/test/sda3/work/cmake/Step8_build
test@test:~/sda3/work/cmake/Step8_build$
test@test:~/sda3/work/cmake/Step8_build$
test@test:~/sda3/work/cmake/Step8_build$ cmake --build .
[ 11%] Building CXX object MathFunctions/CMakeFiles/MakeTable.dir/MakeTable.cxx.o
[ 22%] Linking CXX executable MakeTable
[ 22%] Built target MakeTable
[ 33%] Generating Table.h
[ 44%] Building CXX object MathFunctions/CMakeFiles/SqrtLibrary.dir/mysqrt.cxx.o
[ 55%] Linking CXX static library libSqrtLibrary.a
[ 55%] Built target SqrtLibrary
[ 66%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/MathFunctions.cxx.o
[ 77%] Linking CXX static library libMathFunctions.a
[ 77%] Built target MathFunctions
[ 88%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.o
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
test@test:~/sda3/work/cmake/Step8_build$
3.6 运行结果
test@test:~/sda3/work/cmake/Step8_build$ ./Tutorial 100
Computing sqrt of 100 to be 50.5
Computing sqrt of 100 to be 26.2401
Computing sqrt of 100 to be 15.0255
Computing sqrt of 100 to be 10.8404
Computing sqrt of 100 to be 10.0326
Computing sqrt of 100 to be 10.0001
Computing sqrt of 100 to be 10
Computing sqrt of 100 to be 10
Computing sqrt of 100 to be 10
Computing sqrt of 100 to be 10
The square root of 100 is 10
test@test:~/sda3/work/cmake/Step8_build$
相关文章:
CMake教程-第 8 步:添加自定义命令和生成文件
CMake教程-第 8 步:添加自定义命令和生成文件 1 CMake教程介绍2 学习步骤Step 1: A Basic Starting PointStep 2: Adding a LibraryStep 3: Adding Usage Requirements for a LibraryStep 4: Adding Generator ExpressionsStep 5: Installing and TestingStep 6: Ad…...

快速入门:Spring Cache
目录 一:Spring Cache简介 二:Spring Cache常用注解 2.1:EnableCaching 2.2: Cacheable 2.3:CachePut 2.4:CacheEvict 三:Spring Cache案例 3.1:先在pom.xml中引入两个依赖 3.2:案例 3.2.1:构建数据库表 3.2.2:构建User类 3.2.3:构建Controller mapper层代码 3.…...

探索音频传输系统:数字声音的无限可能 | 百能云芯
音频传输系统是一项关键的技术,已经在数字时代的各个领域中广泛应用,从音乐流媒体到电话通信,再到多媒体制作。本文将深入探讨音频传输系统的定义、工作原理以及在现代生活中的各种应用,以帮助您更好地了解这一重要技术。 音频传输…...

【C++】-c++的类型转换
💖作者:小树苗渴望变成参天大树🎈 🎉作者宣言:认真写好每一篇博客💤 🎊作者gitee:gitee✨ 💞作者专栏:C语言,数据结构初阶,Linux,C 动态规划算法🎄 如 果 你 …...

《论文阅读28》OGMM
一、论文 研究领域: 点云配准 | 有监督 部分重叠论文:Overlap-guided Gaussian Mixture Models for Point Cloud Registration WACV 2023 二、概述 概率3D点云配准方法在克服噪声、异常值和密度变化方面表现出有竞争力的性能。本文将点云对的配准问题…...

忆联分布式数据库存储解决方案,助力MySQL实现高性能、低时延
据艾瑞咨询研究院《2022 年中国数据库研究报告》显示,截止2021年,中国分布式数据库占比达到 20%左右,主要以 MySQL 和 PostgreSQL 为代表的开源数据库为主。MySQL 作为备受欢迎的开源数据库,当前已广泛应用于互联网、金融、交通、…...

网络安全内网渗透之信息收集--systeminfo查看电脑有无加域
systeminfo输出的内容很多,包括主机名、OS名称、OS版本、域信息、打的补丁程序等。 其中,查看电脑有无加域可以快速搜索: systeminfo|findstr "域:" 输出结果为WORKGROUP,可见该机器没有加域: systeminfo…...

MySQL高可用架构学习
MHA(Master HA)是一款开源的由Perl语言开发的MySQL高可用架构方案。它为MySQL 主从复制架构提供了 automating master failover 功能。MHA在监控到 master 节点故障时,会提升其中拥有最新数据的 slave 节点成为新的 master 节点,在…...
seata的AT模式分析
(1)AT模式的核心组件: 事务协调器 TC 维护全局和分支事务的状态; 维护全局锁的状态; 接受TM的提交或者回滚命令,联系RM进行分支事务的提交或者回滚。 事务管理者 TM 开启全局事务,向TC申请…...

【算法练习Day22】 组合总和 III电话号码的字母组合
📝个人主页:Sherry的成长之路 🏠学习社区:Sherry的成长之路(个人社区) 📖专栏链接:练题 🎯长路漫漫浩浩,万事皆有期待 文章目录 组合总和 III剪枝 电话号码…...
react-------JS对象、数组方法实际应用集合
目录 1、向空对象里添加键值对 2、js在数组对象中添加和删除键值对(对象属性)的方法 2.1 添加 3、对已有的数据更换键值对的属性名 4、js字符串拼接、数组转字符串 5、从数组中提取元素 1、向空对象里添加键值对 对象的属性可以使用[ ] 或者 . 而…...
AWS SAP-C02教程6--安全
云的安全是一个重要的问题,很多企业不上云的原因就认为云不安全,特别是对安全性要求较高的企业,所以云安全是一个非常广泛且重要的话题,其实在之前章节中的组件都会或多或少讲述与其相关的安全问题,这里也会详细讲一下。本章主要通过讲述一些独立或与安全有关的组件以及网…...

Go学习第一章——开发环境安装以及快速入门(GoLand)
Go开发环境安装以及快速入门 一、环境配置1.1 go开发工具1.2 go sdk下载3.1 go相关命令行 二、快速入门2.1 创建项目2.2 创建.go程序文件2.3.配置 mod 的开启与关闭2.4 用 GoLand 写第一份代码2.5.代码静态检测(此部分非必要) 三、初步了解3.1 代码解释以…...
大数据学习(14)-Map Join和Common Join
&&大数据学习&& 🔥系列专栏: 👑哲学语录: 承认自己的无知,乃是开启智慧的大门 💖如果觉得博主的文章还不错的话,请点赞👍收藏⭐️留言📝支持一下博>主哦&#x…...

Docker安装ES7.14和Kibana7.14(无账号密码)
一、Docker安装ES7.14.0 1、下载镜像 docker pull elasticsearch:7.14.0 2、docker安装7.14.0 mkdir -p /usr/local/elasticsearch/config mkdir -p /usr/local/elasticsearch/data chmod 777 -R /usr/local/elasticsearch/ echo "http.host: 0.0.0.0" >> /u…...

Zynq中断与AMP~双核串口环回之PS与PL通信
实现思路: 额外配置:通过PL配置计数器,向CPU0和CPU1发送硬中断。 1.串口中断CPU0,在中断中设置接收设置好字长的数据,如果这些数据的数值符合约定的命令,则关闭硬中断,并将这部分数据存入AxiLi…...

【一:实战开发testng的介绍】
目录 1、主要内容1.1、为啥要做接口测试1.2、接口自动化测试落地过程1.3、接口测试范围1.4、手工接口常用的工具1.5、自动化框架的设计 2、testng自动化测试框架基本测试1、基本注解2、忽略测试3、依赖测试4、超时测试5、异常测试6、通过xml文件参数测试7、通过data实现数据驱动…...
C现代方法(第9章)笔记——函数
文章目录 第9章 函数9.1 函数的定义和调用9.1.1 函数定义9.1.2 函数调用 9.2 函数声明9.3 实际参数9.3.1 实际参数的转换9.3.2 数组型实际参数9.3.3 变长数组形式参数(C99)9.3.4 在数组参数声明中使用static(C99)9.3.5 复合字面量 9.4 return语句9.5 程序终止9.5.1 exit函数 9.…...

【算法练习Day23】 复原 IP 地址子集子集 II
📝个人主页:Sherry的成长之路 🏠学习社区:Sherry的成长之路(个人社区) 📖专栏链接:练题 🎯长路漫漫浩浩,万事皆有期待 文章目录 复原 IP 地址子集子集 II总…...

fastadmin框架token验证
在FastAdmin框架中,Token验证是一种常见的身份验证方法,用于确保用户请求的安全性和合法性。本文将介绍如何在FastAdmin框架中实现Token验证。 什么是Token验证? Token验证是一种基于令牌(Token)的身份验证方式。在这种方式下,用…...

微信小程序之bind和catch
这两个呢,都是绑定事件用的,具体使用有些小区别。 官方文档: 事件冒泡处理不同 bind:绑定的事件会向上冒泡,即触发当前组件的事件后,还会继续触发父组件的相同事件。例如,有一个子视图绑定了b…...
《Playwright:微软的自动化测试工具详解》
Playwright 简介:声明内容来自网络,将内容拼接整理出来的文档 Playwright 是微软开发的自动化测试工具,支持 Chrome、Firefox、Safari 等主流浏览器,提供多语言 API(Python、JavaScript、Java、.NET)。它的特点包括&a…...

1.3 VSCode安装与环境配置
进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件,然后打开终端,进入下载文件夹,键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...
Qwen3-Embedding-0.6B深度解析:多语言语义检索的轻量级利器
第一章 引言:语义表示的新时代挑战与Qwen3的破局之路 1.1 文本嵌入的核心价值与技术演进 在人工智能领域,文本嵌入技术如同连接自然语言与机器理解的“神经突触”——它将人类语言转化为计算机可计算的语义向量,支撑着搜索引擎、推荐系统、…...
【服务器压力测试】本地PC电脑作为服务器运行时出现卡顿和资源紧张(Windows/Linux)
要让本地PC电脑作为服务器运行时出现卡顿和资源紧张的情况,可以通过以下几种方式模拟或触发: 1. 增加CPU负载 运行大量计算密集型任务,例如: 使用多线程循环执行复杂计算(如数学运算、加密解密等)。运行图…...
爬虫基础学习day2
# 爬虫设计领域 工商:企查查、天眼查短视频:抖音、快手、西瓜 ---> 飞瓜电商:京东、淘宝、聚美优品、亚马逊 ---> 分析店铺经营决策标题、排名航空:抓取所有航空公司价格 ---> 去哪儿自媒体:采集自媒体数据进…...
大数据学习(132)-HIve数据分析
🍋🍋大数据学习🍋🍋 🔥系列专栏: 👑哲学语录: 用力所能及,改变世界。 💖如果觉得博主的文章还不错的话,请点赞👍收藏⭐️留言Ǵ…...

以光量子为例,详解量子获取方式
光量子技术获取量子比特可在室温下进行。该方式有望通过与名为硅光子学(silicon photonics)的光波导(optical waveguide)芯片制造技术和光纤等光通信技术相结合来实现量子计算机。量子力学中,光既是波又是粒子。光子本…...

HashMap中的put方法执行流程(流程图)
1 put操作整体流程 HashMap 的 put 操作是其最核心的功能之一。在 JDK 1.8 及以后版本中,其主要逻辑封装在 putVal 这个内部方法中。整个过程大致如下: 初始判断与哈希计算: 首先,putVal 方法会检查当前的 table(也就…...

中医有效性探讨
文章目录 西医是如何发展到以生物化学为药理基础的现代医学?传统医学奠基期(远古 - 17 世纪)近代医学转型期(17 世纪 - 19 世纪末)现代医学成熟期(20世纪至今) 中医的源远流长和一脉相承远古至…...