GNU开发工具:CMake快速入门教程(5)

# 测试帮助信息是否可以正常提示
    add_test (test_usage demo)
    set_tests_properties (test_usage
      PROPERTIES PASS_REGULAR_EXPRESSION "Usage: .* base exponent")

# 测试 5 的平方
    # add_test (test_5_2 Demo 5 2)

# set_tests_properties (test_5_2
    #  PROPERTIES PASS_REGULAR_EXPRESSION "is 25")

# 测试 10 的 5 次方
    # add_test (test_10_5 Demo 10 5)

# set_tests_properties (test_10_5
    #  PROPERTIES PASS_REGULAR_EXPRESSION "is 100000")

# 测试 2 的 10 次方
    # add_test (test_2_10 Demo 2 10)

# set_tests_properties (test_2_10
    #  PROPERTIES PASS_REGULAR_EXPRESSION "is 1024")

# 定义一个宏,用来简化测试工作
    macro (do_test arg1 arg2 result)
      add_test (test_${arg1}_${arg2} demo ${arg1} ${arg2})
      set_tests_properties (test_${arg1}_${arg2}
        PROPERTIES PASS_REGULAR_EXPRESSION ${result})
    endmacro (do_test)

# 利用 do_test 宏,测试一系列数据
    do_test (5 2 "is 25")
    do_test (10 5 "is 100000")
    do_test (2 10 "is 1024")

分别指定当前的项目的主版本号和副版本号。
为了在代码中获取版本信息,可以修改 config.h.in 文件,添加两个预定义变量:

// the configured options and settings for Tutorial
#define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@
#define Demo_VERSION_MINOR @Demo_VERSION_MINOR@

// does the platform provide pow function?
#cmakedefine HAVE_POW

直接在源码中使用:

#include <stdio.h>
#include <stdlib.h>
#include <config.h>

#ifdef HAVE_POW
#include <math.h>
#else
#include <MathFunctions.h>
#endif

int main(int argc, char *argv[])
{
    if (argc < 3)
    {
        // print version info
        printf("%s Version %d.%d\n",
              argv[0],
              Demo_VERSION_MAJOR,
              Demo_VERSION_MINOR);
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }

double base = atof(argv[1]);
    int exponent = atoi(argv[2]);

#ifdef HAVE_POW
    printf("Now we use the standard library. \n");
    double result = pow(base, exponent);
#else
    printf("Now we use our own Math library. \n");
    double result = power(base, exponent);
#endif

printf("%g ^ %d is %g\n", base, exponent, result);
    return 0;
}

十一、生成安装包 1、增加CPack模块

CMake提供了一个专门用于打包的工具CPack,用于配置生成各种平台上的安装包,包括二进制安装包和源码安装包。
首先在顶层的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模块。
在顶层目录下创建License.txt文件内如如下:

The MIT License (MIT)

Copyright (c) 2018 Scorpio Studio

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.

2、生成安装包

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/12514.html