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)的身份验证方式。在这种方式下,用…...
synchronized 学习
学习源: https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖,也要考虑性能问题(场景) 2.常见面试问题: sync出…...
基于大模型的 UI 自动化系统
基于大模型的 UI 自动化系统 下面是一个完整的 Python 系统,利用大模型实现智能 UI 自动化,结合计算机视觉和自然语言处理技术,实现"看屏操作"的能力。 系统架构设计 #mermaid-svg-2gn2GRvh5WCP2ktF {font-family:"trebuchet ms",verdana,arial,sans-…...

基于ASP.NET+ SQL Server实现(Web)医院信息管理系统
医院信息管理系统 1. 课程设计内容 在 visual studio 2017 平台上,开发一个“医院信息管理系统”Web 程序。 2. 课程设计目的 综合运用 c#.net 知识,在 vs 2017 平台上,进行 ASP.NET 应用程序和简易网站的开发;初步熟悉开发一…...
Cesium1.95中高性能加载1500个点
一、基本方式: 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享
文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的,根据Excel列的需求预估的工时直接打骨折,不要问我为什么,主要…...

Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)
目录 一、👋🏻前言 二、😈sinx波动的基本原理 三、😈波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、🌊波动优化…...

Springboot社区养老保险系统小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,社区养老保险系统小程序被用户普遍使用,为方…...
LeetCode - 199. 二叉树的右视图
题目 199. 二叉树的右视图 - 力扣(LeetCode) 思路 右视图是指从树的右侧看,对于每一层,只能看到该层最右边的节点。实现思路是: 使用深度优先搜索(DFS)按照"根-右-左"的顺序遍历树记录每个节点的深度对于…...

Docker 本地安装 mysql 数据库
Docker: Accelerated Container Application Development 下载对应操作系统版本的 docker ;并安装。 基础操作不再赘述。 打开 macOS 终端,开始 docker 安装mysql之旅 第一步 docker search mysql 》〉docker search mysql NAME DE…...
纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join
纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...