1、安装测试

  CMake 也可以指定安装规则,以及添加测试。这两个功能分别可以通过在产生 Makefile 后使用 make install 和 make test 来执行。在 GNU Makefile 里,你可能需要为此编写 install 和 test 两个伪目标和相应的规则,但在 CMake 里,这样的工作同样只需要简单的调用几条命令。

  1.1定制安装规则

  首先先在 math/CMakeLists.txt 文件里添加下面两行:# 指定 MathFunctions 库的安装路径install (TARGETS MathFunctions DESTINATION bin)install (FILES MathFunctions.h DESTINATION include)

  指明 MathFunctions 库的安装路径。之后同样修改根目录的 CMakeLists 文件,在末尾添加下面几行:  

# 指定安装路径install (TARGETS Demo DESTINATION bin)install (FILES "${PROJECT_BINARY_DIR}/config.h"DESTINATION include)

  通过上面的定制,生成的 Demo 文件和 MathFunctions 函数库 libMathFunctions.o 文件将会被复制到 /usr/local/bin 中,而 MathFunctions.h 和生成的 config.h 文件则会被复制到 /usr/local/include 中。我们可以验证一下(顺带一提的是,这里的 /usr/local/ 是默认安装到的根目录,可以通过修改 CMAKE_INSTALL_PREFIX 变量的值来指定这些文件应该拷贝到哪个根目录): 

 [root@hackett demo5]# make installConsolidate compiler generated dependencies of target MathFunctions[ 50%] Built target MathFunctionsConsolidate compiler generated dependencies of target demo[100%] Built target demoInstall the project...-- Install configuration: ""-- Installing: /usr/local/bin/demo-- Installing: /usr/local/include/config.h-- Installing: /usr/local/bin/libMathFunctions.a-- Installing: /usr/local/include/myMath.h[root@hackett demo5]# ls /usr/local/bin/demo      libMathFunctions.a               [root@iZwz97bu0gr8vx0j8l6kkzZ demo5]# ls /usr/local/include/config.h myMath.h

  1.2工程添加测试

  添加测试同样很简单。CMake 提供了一个称为 CTest 的测试工具。我们要做的只是在项目根目录的 CMakeLists 文件中调用一系列的 add_test 命令。

  CMakeLists.txtcmake_minimum_required(VERSION 3.10)# set the project nameproject(demo5)# 加入一个配置头文件,用于处理 CMake 对源码的设置configure_file ("${PROJECT_SOURCE_DIR}/config.h.in""${PROJECT_BINARY_DIR}/config.h")# 是否使用自己的 MathFunctions 库option (USE_MYMATH"Use provided math implementation" ON)# 是否加入 MathFunctions 库if (USE_MYMATH)include_directories ("${PROJECT_SOURCE_DIR}/math")add_subdirectory (math)set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)endif (USE_MYMATH)aux_source_directory(. DIR_SRCS)# 指定生成目标add_executable(demo ${DIR_SRCS})target_link_libraries(demo ${EXTRA_LIBS})# 指定安装路径install (TARGETS demo DESTINATION bin)install (FILES "${PROJECT_BINARY_DIR}/config.h"DESTINATION include)enable_testing()# 测试程序是否成功运行add_test (test_run demo 3 2)add_test (test_35_2 demo 35 2)set_tests_properties (test_35_2 PROPERTIES PASS_REGULAR_EXPRESSION "37")add_test (test_5_2 demo 5 2)set_tests_properties (test_5_2 PROPERTIES PASS_REGULAR_EXPRESSION "7")add_test (test_2_3 demo 2 3)set_tests_properties (test_2_3 PROPERTIES PASS_REGULAR_EXPRESSION "5")

  上面的代码包含了四个测试。第一个测试 test_run 用来测试程序是否成功运行并返回 0 值。剩下的三个测试分别用来测试 35 + 2 、5 + 2、2 + 3是否都能得到正确的结果。其中 PASS_REGULAR_EXPRESSION 用来测试输出是否包含后面跟着的字符串。  

测试结果:[root@hackett demo5]# make Consolidate compiler generated dependencies of target MathFunctions[ 50%] Built target MathFunctionsConsolidate compiler generated dependencies of target demo[ 75%] Building CXX object CMakeFiles/demo.dir/main.cpp.o[100%] Linking CXX executable demo[100%] Built target demo[root@hackett demo5]# make testRunning tests...Test project /root/workspace/cmake/demo5Start 1: test_run1/4 Test #1: test_run .........................   Passed    0.00 secStart 2: test_35_22/4 Test #2: test_35_2 ........................   Passed    0.00 secStart 3: test_5_23/4 Test #3: test_5_2 .........................   Passed    0.00 secStart 4: test_2_34/4 Test #4: test_2_3 .........................   Passed    0.00 sec100% tests passed, 0 tests failed out of 4Total Test time (real) =   0.01 sec

  如果要测试更多的输入数据,像上面那样一个个写测试用例未免太繁琐。这时可以通过编写宏来实现:  

# 定义一个宏,用来简化测试工作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 (35 2 "37")do_test (5 52 "7")do_test (2 3 "5")

  关于 CTest 的更详细的用法可以通过 man 1 ctest 参考 CTest 的文档。

CMake实战之安装测试和添加环境生成安装包相关推荐

  1. CMake实战:安装测试和添加环境生成安装包

    1.安装测试 CMake 也可以指定安装规则,以及添加测试.这两个功能分别可以通过在产生 Makefile 后使用 make install 和 make test 来执行.在 GNU Makefil ...

  2. Anaconda3安装pytorch未添加环境变量如何运行xxx.sh脚本

    Anaconda3安装pytorch未添加环境变量如何运行xxx.sh脚本? 通过添加临时环境变量可以解决!!! 1.找到anaconda3的安装位置,例如我的安装位置为:/home/hyw/data ...

  3. 安装vs 2013 与打包项目生成安装包

    一.安装VS2013 安装包地址:https://blog.csdn.net/dr_yangzdy/article/details/80647444 激活码地址:https://jingyan.bai ...

  4. android studio cmake opencv_Mac下安装及配置OpenCV环境(Xcode)

    Mac下安装及配置OpenCV环境(Xcode) OpenCV (Open Source Computer Vision Library) OpenCV是Intel开源计算机视觉库.它由一系列 C 函 ...

  5. Kuberntes云原生实战04 安装前置条件及Docker环境

    大家好,我是飘渺. 今天咱们继续更新Kubernetes云原生实战系列,本节文章主要是给集群安装一些前置环境已经安装容器运行时环境Docker. Kubernetes 已经成为事实上容器编排的标准,D ...

  6. 安装python3(包括更改安装路径、添加环境路径)

    python3的安装 1.到官网(https://www.python.org/downloads/windows/)下载安装包 https://www.python.org/downloads/re ...

  7. iis6 php mysql 一键_一键搞定:php5 环境集成安装包 for IIS6 修正版

    要更新: 1:针对上一个版本所有的问题进行了修复. 2:已经实现了一键安装,无需手动添加应用程序扩展. 3:修正在一些系统平台上面会有phpmyadmin不能登录的现象. ============== ...

  8. 移动安全测试框架-MobSF环境搭建

    文章目录 一.MobSF是什么? 二.环境搭建 1.环境说明 2.静态环境 3.动态环境 4.MobSF搭建 5.遇到到问题 一.MobSF是什么? 移动安全框架(MobSF)是一种自动.一体化的移动 ...

  9. linux下flex与bison源码安装,Win flex-bison开发环境配置

    Flex-Bison是一套很好很方便的工具,但是主要基于Linux,对于我这种喜(懒)欢(于)使(学)用(习) Windows(Linux)的主,希望能够在windows环境中使用这套工具.所幸,国外 ...

最新文章

  1. linux树莓派连接wifi密码,树莓派连接WiFi,不使用界面,多WiFi切换
  2. php启用openssl,php怎么开启openssl模块
  3. Java异常处理-自定义异常
  4. 排序算法专题-插入排序
  5. lableme标注的json文件转为mask r-cnn训练用的coco数据集格式
  6. 线程程序编译错误注意加-lpthread
  7. PKM2 - PKManager (基于内容的个人知识管理工具) 5M 绿色免费
  8. Express全系列教程之(六):cookie的使用
  9. windows主机和虚拟机设置桥接
  10. mysql:本地mysql不能被其他主机连接解决方法
  11. IMSI号和IMEI解释
  12. 各类常见的关系型数据库
  13. webrtc jitter buffer
  14. CryEngineV UI 制作
  15. 新H5中用canvas画一个数字钟表
  16. numpy中的random.choice()函数
  17. dbca asm ora 01017错误
  18. Alpha系列——从MPT到APT
  19. Wiley大数据-大数据开发基础上-笔记
  20. java 求1000以内完数

热门文章

  1. Microbiome: 16S rRNA基因拷贝数应该被校正吗?
  2. 扩增子统计绘图6韦恩图:比较组间共有和特有OTU或分类单元
  3. pandas使用max函数和min函数计算dataframe日期(时间)数据列中最大日期和最小日期对应的数据行(maximum and minimum date or time row)
  4. python使用matplotlib可视化、使用matplotlib可视化scipy.misc图像、自定义使用winter色彩映射、将不同亮度映射到不同的色彩
  5. seaborn可视化条形图并按照升序排序条形图进行可视化:Sort Bars in Barplot in Ascending Order in Python
  6. R语言单向多元方差分析MANOVA(one-way MANOVA)实战:multivariate analysis of variance
  7. R符号秩检验(WILCOXON SIGNED RANK TEST)
  8. ML基石_14_Regularization
  9. 抓图软件_Faststone capture8.3
  10. demo flink写入kafka_Flink结合Kafka实时写入Iceberg实践笔记