Installing and Testing
Installing
使用CMakeLists.txt安装二进制文件、库、头文件:
set(installable_libs MathFunctions tutorial_compiler_flags)
install(TARGETS ${installable_libs} DESTINATION lib)
install(TARGETS Tutorial DESTINATION bin)
安装指定目标到lib文件夹中
install(FILES MathFunctions.h DESTINATION include)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include
)
安装文件到指定目录中
安装指令:
cmake --install . --config Debug --prefix "/home/myuser/installdir"
安装Debug版本相关文件,安装目录的前缀是/home/myuser/installdir
Testing
为可执行文件添加测试。
开启测试
enable_testing()
add_test(NAME Runs COMMAND Tutorial 25)
添加名为Runs的测试,指令为Tutorial 25
add_test(NAME StandardUse COMMAND Tutorial 4)
set_tests_properties(StandardUse
PROPERTIES PASS_REGULAR_EXPRESSION "4 is 2"
)
添加测试,并验证正则是否匹配输出
function(do_test target arg result)
add_test(NAME Comp${arg} COMMAND ${target} ${arg})
set_tests_properties(Comp${arg}
PROPERTIES PASS_REGULAR_EXPRESSION ${result}
)
endfunction()
# do a bunch of result based tests
do_test(Tutorial 4 "4 is 2")
do_test(Tutorial 9 "9 is 3")
do_test(Tutorial 5 "5 is 2.236")
do_test(Tutorial 7 "7 is 2.645")
do_test(Tutorial 25 "25 is 5")
do_test(Tutorial -25 "-25 is (-nan|nan|0)")
do_test(Tutorial 0.0001 "0.0001 is 0.01")
定义函数do_test方便添加测试用例
执行测试用例
ctest -C Debug
指定Debug版本进行测试
本文介绍了如何使用CMakeLists.txt来安装二进制文件、库和头文件,并详细展示了如何配置安装路径。同时,文章还讲解了如何在CMake中启用测试,添加测试用例,以及如何通过ctest运行这些测试,确保程序的正确性。

7665

被折叠的 条评论
为什么被折叠?



