51Testing软件测试论坛
标题:
CMake实战:安装测试和添加环境生成安装包(下)
[打印本页]
作者:
lsekfe
时间:
2022-1-14 13:34
标题:
CMake实战:安装测试和添加环境生成安装包(下)
2、添加版本号
首先修改顶层 CMakeLists 文件,在 project 命令之后加入如下两行:
set (Demo_VERSION_MAJOR 1)
set (Demo_VERSION_MINOR 0)
复制代码
分别指定当前的项目的主版本号和副版本号。
之后,为了在代码中获取版本信息,我们可以修改 config.h.in 文件,添加两个预定义变量:
// the configured options and settings for Tutorial
#define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@
#define Demo_VERSION_MINOR @Demo_VERSION_MINOR@
复制代码
这样就可以直接在代码中打印版本信息了:
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#ifdef USE_MYMATH
#include "math/myMath.h"
#else
#include <math.h>
#endif
int sub(int a, int b) {
return (a - b);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s argv[1] argv[2] \n", argv[0]);
return 1;
}
printf("version : %d.%d\n", Demo_VERSION_MAJOR, Demo_VERSION_MINOR);
int a = atof(argv[1]);
int b = atoi(argv[2]);
#ifdef USE_MYMATH
printf("Now we use our own Math library. \n");
int result = add(a, b);
printf("%d + %d = %d\n", a, b, result);
#else
printf("Now we use the main.cpp sub function. \n");
int result = sub(a, b);
printf("%d - %d = %d\n", a, b, result);
#endif
return 0;
}
复制代码
运行结果:
[root@hackett demo7]# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /root/workspace/cmake/demo7
[root@hackett demo7]# make
Consolidate compiler generated dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/myMath.cpp.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
[ 75%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
[100%] Linking CXX executable demo
[100%] Built target demo
[root@hackett demo7]# ./demo 2 3
version : 1.0
Now we use our own Math library.
2 + 3 = 5
复制代码
3、生成安装包
如何配置生成各种平台上的安装包,包括二进制安装包和源码安装包。为了完成这个任务,我们需要用到 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)
复制代码
上面的代码做了以下几个工作:
1. 导入 InstallRequiredSystemLibraries 模块,以便之后导入 CPack 模块;
2. 设置一些 CPack 相关变量,包括版权信息和版本信息,其中版本信息用了上一节定义的版本号;
3. 导入 CPack 模块。
创建一个``License.txt`文件
touch License.txt
接下来的工作是像往常一样构建工程,并执行 cpack 命令。
生成二进制安装包:
cpack -C CPackConfig.cmake
复制代码
生成源码安装包:
cpack -C CPackSourceConfig.cmake
复制代码
我们可以试一下。在生成项目后,执行 cpack -C CPackConfig.cmake 命令:
[root@hackett demo8]# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /root/workspace/cmake/demo8
[root@hackett demo8]# cpack -C CPackConfig.cmake
CPack: Create package using STGZ
CPack: Install projects
CPack: - Run preinstall target for: demo8
CPack: - Install project: demo8 [CPackConfig.cmake]
CPack: Create package
CPack: - package: /root/workspace/cmake/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 [CPackConfig.cmake]
CPack: Create package
CPack: - package: /root/workspace/cmake/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 [CPackConfig.cmake]
CPack: Create package
CPack: - package: /root/workspace/cmake/demo8/demo8-1.0.1-Linux.tar.Z generated.
[root@hackett demo8]# ls
CMakeCache.txt cmake_install.cmake config.h CPackConfig.cmake CPackSourceConfig.cmake demo demo8-1.0.1-Linux.tar.gz install_manifest.txt main.cpp math
CMakeFiles CMakeLists.txt config.h.in _CPack_Packages CTestTestfile.cmake demo8-1.0.1-Linux.sh demo8-1.0.1-Linux.tar.Z License.txt Makefile
复制代码
这 3 个二进制包文件所包含的内容是完全相同的。我们可以执行其中一个。此时会出现一个由 CPack 自动生成的交互式安装界面:
[root@hackett 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: /root/workspace/cmake/demo8
If you want to stop extracting, please press <ctrl-C>.
Do you accept the license [yn]:
y
By default the demo8 will be installed in:
"/root/workspace/cmake/demo8/demo8-1.0.1-Linux"
Do you want to include the subdirectory demo8-1.0.1-Linux
Saying no will install in: "/root/workspace/cmake/demo8" [Yn]:
y
Using target directory: /root/workspace/cmake/demo8/demo8-1.0.1-Linux
Extracting, please wait...
Unpacking finished successfully
复制代码
完成后提示安装到了 demo8-1.0.1-Linux 子目录中,我们可以进去执行该程序:
[root@hackett demo8]# ls
CMakeCache.txt cmake_install.cmake config.h CPackConfig.cmake CPackSourceConfig.cmake demo demo8-1.0.1-Linux.sh demo8-1.0.1-Linux.tar.Z main.cpp math
CMakeFiles CMakeLists.txt config.h.in _CPack_Packages CTestTestfile.cmake demo8-1.0.1-Linux demo8-1.0.1-Linux.tar.gz install_manifest.txt Makefile
[root@hackett demo8]# ./demo8-1.0.1-Linux/bin/demo 3 2
version : 1.0
Now we use our own Math library.
3 + 2 = 5
复制代码
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2