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 中对 logexp 函数的检查。然后删除 mysqrt.cxx 中的 HAVE_LOGHAVE_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.hmysqrt.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$

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Linux】在Ubuntu下安装Zotero

【Linux】在Ubuntu下安装Zotero 文章目录 【Linux】在Ubuntu下安装Zotero1. Debian InstallationReference 1. Debian Installation 直接使用下面三条语句进行安装即可 wget -qO- https://raw.githubusercontent.com/retorquere/zotero-deb/master/install.sh | sudo bash su…

Fiddler之Replay功能详解

今天就先来看看Fiddler的功能。 Fiddler&#xff0c;最容易看到的就是快捷工具栏中的 Replay 按钮 解释下&#xff1a; Reissue the selected requests. 重发选中的请求 Hold CTRL to reissue unconditionallly. 选中请求按住 CTRL 键&#xff0c;点击Replay时无条件重发选中…

Python——案例

题一&#xff1a;利用装饰器来计算函数的执行时间 代码&#xff1a; import timedef decorated(fn):def inner():a time.time() # func开始的时间 time.time记录时间fn()b time.time() # func结束的时间print(f"{fn.__name__}程序运行的总数时间:{b - a}秒"…

TP5.1 导出excel文件

在 ThinkPHP 5.1 中引入 PHPExcel&#xff08;现在已被官方弃用&#xff0c;推荐使用 PhpSpreadsheet&#xff09;时&#xff0c;可以按照以下步骤进行操作&#xff1a; 在 composer.json 文件中添加 PHPExcel&#xff08;PhpSpreadsheet&#xff09;的依赖项。找到 require 部…

Go 函数的健壮性、panic异常处理、defer 机制

Go 函数的健壮性、panic异常处理、defer 机制 文章目录 Go 函数的健壮性、panic异常处理、defer 机制一、函数健壮性的“三不要”原则1.1 原则一&#xff1a;不要相信任何外部输入的参数1.2 原则二&#xff1a;不要忽略任何一个错误1.3 原则三&#xff1a;不要假定异常不会发生…

04 接口隔离原则

官方定义 <<代码整洁之道>>作者罗伯特 C马丁 为 “接口隔离原则” 的定义是&#xff1a;客户端不 应该被迫依赖于它不使用的方法&#xff08;Clients should not be forced to depend on methods they do not use&#xff09;。 该原则还有另外一个定义&#xff1…

项目中 .env.development 与 .env.production 的区别

文章目录 一、项目中使用此两个文件的意义二、使用方式 一、项目中使用此两个文件的意义 我们在开发项目时&#xff0c;经常会有开发环境与生产环境&#xff0c;分别会对应不同的请求地址与各种不同的变量&#xff0c;这个时候我们就可以使用 process.env 去抓取这两个文件写的…

如何在Android项目中制作和使用三方包(jar文件)

文章目录 1 概念介绍2 制作方法2.1 制作步骤2.2 制作结果3 使用方法3.1 具体步骤3.2 示例代码4 内容总结在项目中为了跨部门协作需要把相关的内容打成包文件,基于这个需求,我们将介绍如何把 代码制作成三方包,这里的三方包是指jar文件。同时也会介绍如何在Android项目中使用…

win10_Git基于WSL(Linux子系统)统计代码行数命令

win10_Git基于WSL&#xff08;Linux子系统&#xff09;统计代码行数命令 一、引言 找到的基于git统计代码行数的命令&#xff0c;一般都是基于linux系统的shell命令。在使用mac电脑或者linux系统开发时&#xff0c;执行这些命令比较方便。 但是还有大部分人是使用windows做开…

Lua快速入门教程

文章目录 1、Linux安装Lua2、语法练习2.1、变量2.2、循环2.3、函数2.4、数组2.5、迭代器2.6、Table操作2.7、Lua 模块与包2.8、加载机制2.9、Lua 元表(Metatable) 3、Lua 协同程序(coroutine)4、文件IO操作4.1、简单模式4.2、完全模式 5、错误处理 内容来源菜鸟教程&#xff0c…

【LeetCode-数组】-- 寻找数组的中心索引

寻找数组的中心索引 class Solution {public int pivotIndex(int[] nums) {int n nums.length,sum 0;for(int i 0;i<n;i){ //计算所有元素之和sumnums[i];}int sum_l 0; //统计左边元素之和for(int i 0;i<n;i){sum - nums[i];if(sum_l sum){return i;}sum_l nums…

并发编程之互斥锁

互斥锁 锁在IT界都是非常重要的&#xff0c;不但在Python中出现&#xff0c;尤其是数据库中得锁更多&#xff0c; 比如&#xff1a;表锁、行锁、悲观锁、乐观锁、进程锁、互斥锁、递归锁、可重入锁、死锁等。 互斥锁是什么&#xff1f; 将并发变成串行 虽然牺牲了程序的执行效率…

RT thread 信号量操作

信号量控制 在RT-Thread 中&#xff0c;信号量控制块是操作系统用于管理信号量的一个数据结构。 struct rt_semaphore { struct rt_ipc_object parent; /**<inherit from ipc_object*/ rt_uint16_t value; /**<value of semaphore.*/ }定义静态信号量:struct rt_semaph…

五、K8S之Service

Kubernetes Service 一、概念 Deployment对象部署完应用还需要向外界暴露入口才能通过HTTP访问到K8S集群里的应用Pod。Service就是做这件事情的&#xff0c;为什么还需要一个这样的API对象&#xff0c;一个方面是因为Pod的IP不是固定的&#xff0c;另外一个方面是因为一组Pod…

Linux文件管理与用户管理

一、查看文件内容 1、回顾之前的命令 cat命令、tac命令、head命令、tail命令、扩展&#xff1a;tail -f动态查看一个文件的内容 2、more分屏显示文件内容&#xff08;了解&#xff09; 基本语法&#xff1a; # more 文件名称 特别注意&#xff1a;more命令在加载文件时并不…

【三:Mock服务的使用】

目录 1、工具包2、mock的demo1、get请求2、post请求3、带cookies的请求4、带请求头的请求5、请求重定向 1、工具包 1、&#xff1a;服务包的下载 moco-runner-0.11.0-standalone.jar 下载 2、&#xff1a;运行命令java -jar ./moco-runner-0.11.0-standalone.jar http -p 888…

2023-10-17 mysql-innodb-解析write_row的record的一行数据-分析

摘要: 2023-10-17 mysql-innodb-解析write_row的record的一行数据-分析. record是一行数据的序列化后的一整个字节流, 在innodb中需要解读出字段. 本文分析如何解析record, 以便学习这种技巧. row_mysql_store_col_in_innobase_format 调用堆栈: #0 row_mysql_store_col_in…

边写代码边学习之mlflow

1. 简介 MLflow 是一个多功能、可扩展的开源平台&#xff0c;用于管理整个机器学习生命周期的工作流程和工件。 它与许多流行的 ML 库内置集成&#xff0c;但可以与任何库、算法或部署工具一起使用。 它被设计为可扩展的&#xff0c;因此您可以编写插件来支持新的工作流程、库和…

【算法学习】归并算法Merge Sort总结

归并排序思路简单&#xff0c;速度仅次于快速排序&#xff0c;为稳定排序算法&#xff0c;一般用于对总体无序&#xff0c;但是各子项相对有序的数列。 1. 基本思想 归并排序使用分治思想&#xff0c;分治模式下每一层递归有三个步骤&#xff1a; 分解&#xff08;divide)&a…

Git命令在线练习网址--非常友好的提示及动画展示

Git命令在线练习 https://learngitbranching.js.org/ 举个栗子: 在练习时会给你相应提示,你可以按照相应步骤进行操作,并且每一步都有动画演示