1.安装涉及到两部分:一是库的安装;另一个是主程序的安装;
库函数包括库与头文件;
- MathFunctions目录下CMakeLists.txt内容
add_library(MathFunctions mysqrt.cxx)
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)
主程序包含主程序与配置文件
- 主程序CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
#set the project name and version
project(Tutorial VERSION 1.0)
#specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
option(USE_MYMATH "Use tutorial provided math implementation" ON)
configure_file(TutorialConfig.h.in TutorialConfig.h)
if(USE_MYMATH)
#add the MathFunctions Library
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
#add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include
)
2. 编译与安装步骤
- 编译
cmake --build . --config Release
- 安装
cmake --install .
3. 增加测试,在主程序的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 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")
4. 编译与测试
- cmake --build . --config Release
- ctest -VV -C Release
E:\QtProjects\cmakeproj\step4\build>ctest -VV -C Release
UpdateCTestConfiguration from :E:/QtProjects/cmakeproj/step4/build/DartConfiguration.tcl
UpdateCTestConfiguration from :E:/QtProjects/cmakeproj/step4/build/DartConfiguration.tcl
Test project E:/QtProjects/cmakeproj/step4/build
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
Start 1: Usage
1: Test command: E:\QtProjects\cmakeproj\step4\build\Release\Tutorial.exe
1: Test timeout computed to be: 10000000
1: E:/QtProjects/cmakeproj/step4/build/Release/Tutorial.exe Version 1.0
1: Usage: E:/QtProjects/cmakeproj/step4/build/Release/Tutorial.exe number
1/8 Test #1: Usage ............................ Passed 0.01 sec
test 2
Start 2: Comp4
2: Test command: E:\QtProjects\cmakeproj\step4\build\Release\Tutorial.exe "4"
2: Test timeout computed to be: 10000000
2: use self defined sqrt lib
2: 4 is 2
2/8 Test #2: Comp4 ............................ Passed 0.01 sec
test 3
Start 3: Comp9
3: Test command: E:\QtProjects\cmakeproj\step4\build\Release\Tutorial.exe "9"
3: Test timeout computed to be: 10000000
3: use self defined sqrt lib
3: 9 is 3
3/8 Test #3: Comp9 ............................ Passed 0.01 sec
test 4
Start 4: Comp5
4: Test command: E:\QtProjects\cmakeproj\step4\build\Release\Tutorial.exe "5"
4: Test timeout computed to be: 10000000
4: use self defined sqrt lib
4: 5 is 2.23607
4/8 Test #4: Comp5 ............................ Passed 0.01 sec
test 5
Start 5: Comp7
5: Test command: E:\QtProjects\cmakeproj\step4\build\Release\Tutorial.exe "7"
5: Test timeout computed to be: 10000000
5: use self defined sqrt lib
5: 7 is 2.64575
5/8 Test #5: Comp7 ............................ Passed 0.01 sec
test 6
Start 6: Comp25
6: Test command: E:\QtProjects\cmakeproj\step4\build\Release\Tutorial.exe "25"
6: Test timeout computed to be: 10000000
6: use self defined sqrt lib
6: 25 is 5
6/8 Test #6: Comp25 ........................... Passed 0.01 sec
test 7
Start 7: Comp-25
7: Test command: E:\QtProjects\cmakeproj\step4\build\Release\Tutorial.exe "-25"
7: Test timeout computed to be: 10000000
7: use self defined sqrt lib
7: -25 is -nan(ind)
7/8 Test #7: Comp-25 .......................... Passed 0.01 sec
test 8
Start 8: Comp0.0001
8: Test command: E:\QtProjects\cmakeproj\step4\build\Release\Tutorial.exe "0.0001"
8: Test timeout computed to be: 10000000
8: use self defined sqrt lib
8: 0.0001 is 0.01
8/8 Test #8: Comp0.0001 ....................... Passed 0.01 sec
100% tests passed, 0 tests failed out of 8
Total Test time (real) = 0.11 sec