lsekfe 发表于 2022-1-14 13:34:25

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 argv \n", argv);
        return 1;
      }
      printf("version : %d.%d\n", Demo_VERSION_MAJOR, Demo_VERSION_MINOR);
      int a = atof(argv);
      int b = atoi(argv);
  #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;
  }运行结果:
 # cmake .
  -- Configuring done
  -- Generating done
  -- Build files have been written to: /root/workspace/cmake/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
   Linking CXX executable demo
   Built target demo
  # ./demo 2 3
  version : 1.0
  Now we use our own Math library.
  2 + 3 = 53、生成安装包
  如何配置生成各种平台上的安装包,包括二进制安装包和源码安装包。为了完成这个任务,我们需要用到 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 命令:
# cmake .
  -- Configuring done
  -- Generating done
  -- Build files have been written to: /root/workspace/cmake/demo8
  # cpack -C CPackConfig.cmake
  CPack: Create package using STGZ
  CPack: Install projects
  CPack: - Run preinstall target for: demo8
  CPack: - Install project: demo8
  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
  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
  CPack: Create package
  CPack: - package: /root/workspace/cmake/demo8/demo8-1.0.1-Linux.tar.Z generated.
  # ls
  CMakeCache.txtcmake_install.cmakeconfig.h   CPackConfig.cmakeCPackSourceConfig.cmakedemo                  demo8-1.0.1-Linux.tar.gzinstall_manifest.txtmain.cppmath
  CMakeFiles      CMakeLists.txt       config.h.in_CPack_Packages    CTestTestfile.cmake      demo8-1.0.1-Linux.shdemo8-1.0.1-Linux.tar.Z   License.txt         Makefile这 3 个二进制包文件所包含的内容是完全相同的。我们可以执行其中一个。此时会出现一个由 CPack 自动生成的交互式安装界面:
# 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:
  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" :
  y
  Using target directory: /root/workspace/cmake/demo8/demo8-1.0.1-Linux
  Extracting, please wait...
  Unpacking finished successfully完成后提示安装到了 demo8-1.0.1-Linux 子目录中,我们可以进去执行该程序:
 # ls
  CMakeCache.txtcmake_install.cmakeconfig.h   CPackConfig.cmakeCPackSourceConfig.cmakedemo               demo8-1.0.1-Linux.sh      demo8-1.0.1-Linux.tar.Zmain.cppmath
  CMakeFiles      CMakeLists.txt       config.h.in_CPack_Packages    CTestTestfile.cmake      demo8-1.0.1-Linuxdemo8-1.0.1-Linux.tar.gzinstall_manifest.txt   Makefile
  # ./demo8-1.0.1-Linux/bin/demo 3 2
  version : 1.0
  Now we use our own Math library.
  3 + 2 = 5






页: [1]
查看完整版本: CMake实战:安装测试和添加环境生成安装包(下)