CMake入门实战——生成安装包

时间:2022-07-22
本文章向大家介绍CMake入门实战——生成安装包,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

生成安装包

本节将学习如何配置生成各种平台上的安装包,包括二进制安装包和源码安装包。为了完成这个任务,我们需要用到 CPack ,它同样也是由 CMake 提供的一个工具,专门用于打包。 首先在顶层的 CMakeLists.txt 文件尾部添加下面几行:

# 构建一个 CPack 安装包
include (InstallRequiredSystemLibraries)
set (CPACK_RESOURCE_FILE_LICENSE
  "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${Demo_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${Demo_VERSION_MINOR}")
include (CPack)

上面的代码做了以下几个工作:

  • 导入 InstallRequiredSystemLibraries 模块,以便之后导入 CPack 模块;
  • 设置一些 CPack 相关变量,包括版权信息和版本信息,其中版本信息用了上一节定义的版本号;
  • 导入 CPack 模块。

接下来的工作是像往常一样构建工程,并执行 cpack 命令。

  • 生成二进制安装包:
cpack -C CPackConfig.cmake
  • 生成源码安装包
cpack -C CPackSourceConfig.cmake

我们可以试一下。在生成项目后,执行 cpack -C CPackConfig.cmake 命令:

xuke@ubuntu:~/work/cmake-demo/Demo8$ ls
CMakeLists.txt  config.h.in  License.txt  main.cc  math
xuke@ubuntu:~/work/cmake-demo/Demo8$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pow
-- Looking for pow - not found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xuke/work/cmake-demo/Demo8
xuke@ubuntu:~/work/cmake-demo/Demo8$ cpack -C CPackSourceConfig.cmake
CPack: Create package using STGZ
CPack: Install projects
CPack: - Run preinstall target for: Demo8
CPack: - Install project: Demo8
CPack: Create package
CPack: - package: /home/xuke/work/cmake-demo/Demo8/Demo8-1.0.1-Linux.sh generated.
CPack: Create package using TGZ
CPack: Install projects
CPack: - Run preinstall target for: Demo8
CPack: - Install project: Demo8
CPack: Create package
CPack: - package: /home/xuke/work/cmake-demo/Demo8/Demo8-1.0.1-Linux.tar.gz generated.
CPack: Create package using TZ
CPack: Install projects
CPack: - Run preinstall target for: Demo8
CPack: - Install project: Demo8
CPack: Create package
CPack: - package: /home/xuke/work/cmake-demo/Demo8/Demo8-1.0.1-Linux.tar.Z generated.

此时会在该目录下创建 3 个不同格式的二进制包文件:

xuke@ubuntu:~/work/cmake-demo/Demo8$ ls
CMakeCache.txt       _CPack_Packages           install_manifest.txt
CMakeFiles           CPackSourceConfig.cmake   License.txt
cmake_install.cmake  CTestTestfile.cmake       main.cc
CMakeLists.txt       Demo                      Makefile
config.h             Demo8-1.0.1-Linux.sh      math
config.h.in          Demo8-1.0.1-Linux.tar.gz
CPackConfig.cmake    Demo8-1.0.1-Linux.tar.Z
xuke@ubuntu:~/work/cmake-demo/Demo8$ ls Demo8-*
Demo8-1.0.1-Linux.sh  Demo8-1.0.1-Linux.tar.gz  Demo8-1.0.1-Linux.tar.Z

这 3 个二进制包文件所包含的内容是完全相同的。我们可以执行其中一个。此时会出现一个由 CPack 自动生成的交互式安装界面:

xuke@ubuntu:~/work/cmake-demo/Demo8$ sh Demo8-1.0.1-Linux.sh
Demo8 Installer Version: 1.0.1, Copyright (c) Humanity
This is a self-extracting archive.
The archive will be extracted to: /home/xuke/work/cmake-demo/Demo8

If you want to stop extracting, please press <ctrl-C>.
The MIT License (MIT)

Copyright (c) 2013 Joseph Pan(http://hahack.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Do you accept the license? [yN]:
y
By default the Demo8 will be installed in:
  "/home/xuke/work/cmake-demo/Demo8/Demo8-1.0.1-Linux"
Do you want to include the subdirectory Demo8-1.0.1-Linux?
Saying no will install in: "/home/xuke/work/cmake-demo/Demo8" [Yn]:
y

Using target directory: /home/xuke/work/cmake-demo/Demo8/Demo8-1.0.1-Linux
Extracting, please wait...

Unpacking finished successfully

完成后提示安装到了 Demo8-1.0.1-Linux 子目录中,我们可以进去执行该程序:

xuke@ubuntu:~/work/cmake-demo/Demo8/Demo8-1.0.1-Linux$ tree
.
├── bin
│   └── Demo
├── include
│   ├── config.h
│   └── MathFunctions.h
└── lib
    └── libMathFunctions.a

3 directories, 4 files
xuke@ubuntu:~/work/cmake-demo/Demo8/Demo8-1.0.1-Linux$ ./bin/Demo 3 2
Now we use our own Math library.
3 ^ 2 is 9

参考

[CMake 入门实战]http://www.hahack.com/codes/cmake/ [代码参考]https://github.com/wzpan/cmake-demo