cmake学习笔记(四)
安装和测试
对于MathFunctions安装库文件和头文件,对于整个应用安装可执行问价和配置文件。
因此,在MathFunctions/CMakeLists.txt中添加:
install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)
在上层CMakeLists.txt中添加:
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include
)
安装指令:
# 指定安装目录
cmake -D CMAKE_INSTALL_PREFIX=/opt/cpp-netlib ..
make
make install
测试:
在上层CMakelists.txt文件末尾添加以下代码测试程序是否执行正常:
enable_testing()
# does the application run
add_test(NAME Runs COMMAND Tutorial 25)
# does the usage message work?
add_test(NAME Usage COMMAND Tutorial)
set_tests_properties(Usage
PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
)
# define a function to simplify adding tests
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_test)
# 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")
第一个测试验证应用程序是否运行、是否发生故障或崩溃以及是否有0返回值。
第二个测试使用PASS_REGULAR_EXPRESSION属性来验证测试的输出是够包含某些字符串。
最后,定义了一个do_test函数。
运行测试程序:
ctest -N
或
ctest -VV
或
make test