当前位置: 首页 > news >正文

CMake Tutorial 巡礼(7)_打包一个安装文件

CMake Tutorial 巡礼(7)_ 打包一个安装文件

这是本系列的第八篇。
上一篇我们学习了如何添加自定义命令及生成文件。
本篇我们要学习一下如何打包一个安装文件,这与CMake Tutorial(4)_安装与测试中提到的略有区别。那一篇中是通过源码直接编译安装,这一篇将会通过cpack打包。这一篇中打包的安装文件将会包含所有的系统依赖库,这样就可以真正地部署到其他非开发环境中去。

本章导读

在这里插入图片描述

第七步 打包一个安装文件

Next suppose that we want to distribute our project to other people so that they can use it. We want to provide both binary and source distributions on a variety of platforms. This is a little different from the install we did previously in Installing and Testing, where we were installing the binaries that we had built from the source code. In this example we will be building installation packages that support binary installations and package management features. To accomplish this we will use CPack to create platform specific installers. Specifically we need to add a few lines to the bottom of our top-level CMakeLists.txt file.

接下来假设我们想要把我们的项目部署到别人那里让别人使用。我们想要向许多不同的平台提供二进制文件和源文件。这与我们之前在Installing and Testing中做的有些许区别,当时我们通过编译源代码的方式编译生成了安装包。在本例中我们将要编译一个支持二进制安装的安装包,并且增加一些管理特性。为了实现这个目的我们使用CPack工具来创建平台相关的安装包。特别地我们需要在顶层的CMakeLists.txt文件末尾添加一些行。

CMakeLists.txt

include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)

小白按:尽管没有必要,但是小白还是贴出这个文件的完整代码:

cmake_minimum_required(VERSION 3.10)

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

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# 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
if(USE_MYMATH)
  add_subdirectory(MathFunctions)
  list(APPEND EXTRA_LIBS MathFunctions)
endif()

# add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

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

# add the install targets
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  DESTINATION include
  )

# enable testing
enable_testing()

# does the application run
add_test(NAME Runs COMMAND Tutorial 25)

# does the usage message work?
add_test(NAME Usage COMMAND Tutorial)
set_tests_properties(Usage
  PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
  )

# define a function to simplify adding tests
function(do_test target arg result)
  add_test(NAME Comp${arg} COMMAND ${target} ${arg})
  set_tests_properties(Comp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result}
    )
endfunction()

# do a bunch of result based tests
do_test(Tutorial 4 "4 is 2")
do_test(Tutorial 9 "9 is 3")
do_test(Tutorial 5 "5 is 2.236")
do_test(Tutorial 7 "7 is 2.645")
do_test(Tutorial 25 "25 is 5")
do_test(Tutorial -25 "-25 is (-nan|nan|0)")
do_test(Tutorial 0.0001 "0.0001 is 0.01")

include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)

That is all there is to it. We start by including InstallRequiredSystemLibraries. This module will include any runtime libraries that are needed by the project for the current platform. Next we set some CPack variables to where we have stored the license and version information for this project. The version information was set earlier in this tutorial and the License.txt has been included in the top-level source directory for this step. The CPACK_SOURCE_GENERATOR variable selects a file format for the source package.

这就是我们需要做的一切了。我们从包含 InstallRequiredSystemLibraries开始。这个模块将要包含对于当前平台项目所需要的所有的运行库。接下来我们设置一些CPack变量,用以指定我们为项目存储license和版本信息的路径。版本信息在tutorial中早就设置好了,License.txt在这一步的顶层源码路径中也已包含在内。 CPACK_SOURCE_GENERATOR 变量则为源码包选定了一种文件格式。

Finally we include the CPack module which will use these variables and some other properties of the current system to setup an installer.

最终我们包含了 CPack module ,足以使用这些变量及当前系统具备的一些其他特性来创建安装包。

The next step is to build the project in the usual manner and then run the cpack executable. To build a binary distribution, from the binary directory run:

接下来一个步骤是用惯常的方式编译项目,然后运行cpack可执行文件。为了编译一个二进制安装包,在二进制路径下执行:

cpack

小白按:熟悉的坑点再次出现,还好在CMake Tutorial 巡礼(4)_安装与测试中这个坑我们已经踩过了。这里所 谓的“二进制路径”,指的再次是CMake在系统中的安装路径,对于小白来说,安装位置在C:\Program Files\CMake\bin路径下。

To specify the generator, use the -G option. For multi-config builds, use -C to specify the configuration. For example:

为了指定生成器,使用-G选项。若是为了多参数编译,使用-C来指定参数,例如:

cpack -G ZIP -C Debug

小白按:小白贴出完整命令:

"C:\Program Files\CMake\bin\cpack" -G ZIP -C Debug

生成得到的安装包在一个名为_CPack_Packages的目录下。
在这里插入图片描述

其路径下有一个win64\ZIP文件夹,其中包含有安装包
在这里插入图片描述

进入到bin目录下可以看到安装包内包含了依赖的系统库:
在这里插入图片描述

For a list of available generators, see cpack-generators(7) or call cpack --help. An archive generator like ZIP creates a compressed archive of all installed files.

若是想查看可用的生成器列表,请参阅 cpack-generators(7)或者调用cpack --help。一个archive generator,例如ZIP创建一个关于所有安装文件的压缩包。

To create an archive of the full source tree you would type:

若是想要创建所有源码的存档,你可以键入:

cpack --config CPackSourceConfig.cmake

小白按:同样地,这里的命令应该是:

"C:\Program Files\CMake\bin\cpack" --config CPackSourceConfig.cmake

小白用的是windows系统,但是这句命令打包的格式是linux的压缩包文件,后缀是tar.gz
里面是所有的源码。

Alternatively, run make package or right click the Package target and Build Project from an IDE.

可选地,亦可在目标包上右键使用make package,从IDE编译项目。

Run the installer found in the binary directory. Then run the installed executable and verify that it works.

运行在二进制路径下找到的安装包。然后执行可执行文件,验证它是否能够正常工作。

小白按:这里的验证留给读者你来进行了,只要注意路径位置是在Step7_build\_CPack_Packages\win64\ZIP\Tutorial-1.0-win64\bin下。

下一篇我们将继续学习“为测试白板添加支持”。

【水平所限,错漏难免,创作不易,轻喷勿骂】

在这里插入图片描述

相关文章:

  • 猿创征文 | 【Java进阶】详解抽象类及常用接口
  • 【SQL刷题】DAY17----SQL表与索引操作专项练习
  • 天龙八部刷马贼和反贼所有坐标
  • 【Django】开发日报_3_Day:员工管理系统
  • 《Go Web 编程》之第5章 内容展示
  • linux环境搭建nacos集群详解
  • 【Linux】进程概念(下篇) —— 程序地址空间详解
  • Windows上部署Discuz论坛
  • 猿创征文|我的C/C++技术成长之路
  • 微信小程序在线考试项目开发-用户信息注册登录功能
  • 微服务项目:尚融宝(36)(核心业务流程:用户绑定(1))
  • 2023秋招的第一个意向书
  • 正点原子Linux MINI板系统固化(烧录uboot、linux kernel、.dtb(设备树)和 rootfs)
  • 核酸检测小程序实战教程
  • MATLAB | 绘制博士学位的图解指南
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 10个确保微服务与容器安全的最佳实践
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • avalon2.2的VM生成过程
  • const let
  • golang 发送GET和POST示例
  • Java新版本的开发已正式进入轨道,版本号18.3
  • jquery ajax学习笔记
  • JS+CSS实现数字滚动
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • Promise初体验
  • webpack+react项目初体验——记录我的webpack环境配置
  • 基于web的全景—— Pannellum小试
  • 今年的LC3大会没了?
  • 使用docker-compose进行多节点部署
  • 使用权重正则化较少模型过拟合
  • 微信小程序:实现悬浮返回和分享按钮
  • 最简单的无缝轮播
  • const的用法,特别是用在函数前面与后面的区别
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​secrets --- 生成管理密码的安全随机数​
  • !$boo在php中什么意思,php前戏
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • (+4)2.2UML建模图
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (2)STM32单片机上位机
  • (C语言)字符分类函数
  • (Git) gitignore基础使用
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (安卓)跳转应用市场APP详情页的方式
  • (强烈推荐)移动端音视频从零到上手(上)
  • (三)Honghu Cloud云架构一定时调度平台
  • **CI中自动类加载的用法总结
  • .NET CF命令行调试器MDbg入门(一)
  • .Net Core 中间件验签
  • .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化
  • .net网站发布-允许更新此预编译站点
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken
  • // an array of int