CMake4:安装与测试

来源:互联网 发布:图片编辑字幕软件 编辑:程序博客网 时间:2024/06/01 09:02

1.前言

为之前的项目添加安装规则install rules和测试支持testing support

2.安装规则installing rules

安装规则是相当直接的了。对于我们设置的MathFunctions库和头文件,通过在MathFunctions的CMakeLists.txt中添加如下两行代码:
install (TARGETS MathFunctions DESTINATION bin)install (FILES MathFunctions.h DESTINATION include)
然后,在顶层CMakeLists.txt文件添加如下代码安装可执行程序,并配置头文件:
# add the install targetsinstall (TARGETS Tutorial DESTINATION bin)install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include)
这就是全部了。接下来就能够组建tutorial,然后利用‘make’进行安装(也可以在IDE中组建INSTALL目标)。
这样就会安装盛和的头文件、库文件和可执行文件。
注意:CMake变量‘CMake_INSTALL_PREFIX’是决定将文件安装在哪个目录下。

3.测试支持Testing Support

测试也是一个非常直接的步骤。
在顶层CMakeList.txt文件最后,我们可以添加大量的基础测试代码,用来验证应用程序是否正常工作。
include(CTest)# does the application runadd_test (TutorialRuns Tutorial 25)# does it sqrt of 25add_test (TutorialComp25 Tutorial 25)set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")# does it handle negative numbersadd_test (TutorialNegative Tutorial -25)set_tests_properties (TutorialNegative PROPERTIES PASS_REGULAR_EXPRESSION "-25 is 0")# does it handle small numbersadd_test (TutorialSmall Tutorial 0.0001)set_tests_properties (TutorialSmall PROPERTIES PASS_REGULAR_EXPRESSION "0.0001 is 0.01")# does the usage message work?add_test (TutorialUsage Tutorial)set_tests_properties (TutorialUsage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number")
组建之后,就可以运行‘ctest’命令行工具运行这些测试代码。
‘PASS_REGULAR_EXPRESSION’测试应用程序输出包含某些字符串。
如果我们希望增加大量的测试代码来测试不同的输入值,我们可以考虑创建一个宏,示例如下:
#define a macro to simplify adding tests, then use itmacro (do_test arg result)  add_test (TutorialComp${arg} Tutorial ${arg})  set_tests_properties (TutorialComp${arg}    PROPERTIES PASS_REGULAR_EXPRESSION ${result})endmacro (do_test) # do a bunch of result based testsdo_test (25 "25 is 5")do_test (-25 "-25 is 0")

4.调试与测试

  • ~/CMakeLists.txt
cmake_minimum_required (VERSION 2.6)project (Tutorial)# The version number.set (Tutorial_VERSION_MAJOR 1)set (Tutorial_VERSION_MINOR 0)# should we use our own math functionsoption(USE_MYMATH "Use tutorial provided math implementation" ON)# configure a header file to pass some of the CMake settings# to the source codeconfigure_file (  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"  "${PROJECT_BINARY_DIR}/TutorialConfig.h"  )# add the binary tree to the search path for include files# so that we will find TutorialConfig.hinclude_directories ("${PROJECT_BINARY_DIR}")# add the MathFunctions library?if (USE_MYMATH)  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")  add_subdirectory (MathFunctions)  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)endif ()# add the executableadd_executable (Tutorial tutorial.c)target_link_libraries (Tutorial  ${EXTRA_LIBS})# add the install targetsinstall (TARGETS Tutorial DESTINATION bin)install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"  DESTINATION include)# enable testingenable_testing ()# does the application runadd_test (TutorialRuns Tutorial 25)# does it sqrt of 25add_test (TutorialComp25 Tutorial 25)set_tests_properties (TutorialComp25  PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5"  )# does it handle negative numbersadd_test (TutorialNegative Tutorial -25)set_tests_properties (TutorialNegative  PROPERTIES PASS_REGULAR_EXPRESSION "-25 is 0"  )# does it handle small numbersadd_test (TutorialSmall Tutorial 0.0001)set_tests_properties (TutorialSmall  PROPERTIES PASS_REGULAR_EXPRESSION "0.0001 is 0.01"  )# does the usage message work?add_test (TutorialUsage Tutorial)set_tests_properties (TutorialUsage  PROPERTIES  PASS_REGULAR_EXPRESSION "Usage:.*number"  )
  • ~/Tutorial.c
// A simple program that computes the square root of a number#include "TutorialConfig.h"#include <math.h>#include <stdio.h>#include <stdlib.h>#ifdef USE_MYMATH#include "MathFunctions.h"#endifint main(int argc, char* argv[]){  double inputValue = atof(argv[1]); //根据编译器的要求,变量要放在最前面  double outputValue = 0;  if (argc < 2) {    fprintf(stdout, "%s Version %d.%d\n", argv[0], Tutorial_VERSION_MAJOR,Tutorial_VERSION_MINOR);fprintf(stdout, "Usage: %s number\n", argv[0]);return 1;  }  if (inputValue >= 0) {#ifdef USE_MYMATH    outputValue = mysqrt(inputValue);#else    outputValue = sqrt(inputValue);#endif  }  fprintf(stdout, "The square root of %g is %g\n", inputValue, outputValue);  return 0;}
  • ~/TutorialConfig.h.in
// the configured options and settings for Tutorial#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@#cmakedefine USE_MYMATH
  • ~/MathFunctions/CMakeLists.txt
add_library(MathFunctions mysqrt.c)install (TARGETS MathFunctions DESTINATION bin)install (FILES MathFunctions.h DESTINATION include
  • ~/MathFunctions/MathFunctions.h
double mysqrt(double x);
  • ~/MathFunctions/mysqrt.c
#include "MathFunctions.h"#include <stdio.h>// a hack square root calculation using simple operationsdouble mysqrt(double x){  double result; //根据编译器要求,所有变量的定义放在最前面  double delta;  int i;    if (x <= 0) {    return 0;  }  result = x;  // do ten iterations  for (i = 0; i < 10; ++i) {    if (result <= 0) {      result = 0.1;    }    delta = x - (result * result);    result = result + 0.5 * delta / result;    fprintf(stdout, "Computing sqrt of %g to be %g\n", x, result);  }  return result;}
Step1:CMake进行Configure & Generate
Step2:Visual Studio进行组建ALL_BUILD
Step3:Visual Studio进行安装INSTALL
~/bin;~/include;~/lib文件夹会出现在CMake预定义的目标目录中,例如我的就在C:/ProgramFile