通過編寫CMakeLists.txt,可以控制生成的Makefile,從而控制編譯過程。CMake自動生成的Makefile不僅可以通過make命令構建項目生成目標文件,還支持安裝(make install)、測試安裝的程序是否能正確執行(make test,或者ctest)、生成當前平臺的安裝包(make package)、生成源碼包(make package_source)、產生Dashboard顯示數據並上傳等高級功能,只要在CMakeLists.txt中簡單配置,就可以完成很多複雜的功能,包括寫測試用例。
CMake使用步驟:
cmake-2.8.1-win32-x86/bin/cmake-gui.exe
執行Configure:
運行之後,生成了如下文件:
生成Makefile:
執行Generate之後生成如下文件:
簡單的程序編譯。
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step1/build3>make
Linking CXX executable Tutorial.exe
[100%] Built target Tutorial
Tutorial.exe Version 1.0
Usage: Tutorial.exe number
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step1/build3>Tutorial.exe 4
The square root of 4 is 2
2、Step2
把子目錄編譯爲庫,並且鏈接到最終的可執行文件。
add_subdirectory (MathFunctions) # 使得子目錄MathFunctions也能被編譯
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
產生makefile:
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step2/build>make
[ 50%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cxx
.obj
Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Linking CXX executable Tutorial.exe
[100%] Built target Tutorial
3、Step3
macro (do_test arg result)
add_test (TutorialComp${arg} Tutorial ${arg})
set_tests_properties (TutorialComp${arg}
PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")
執行make install:
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build>make install
[ 50%] "Built target MathFunctions"
[100%] "Built target Tutorial"
Install the project...
-- Install configuration: ""
-- Installing: C:/Program Files/Tutorial/bin/Tutorial.exe
-- Installing: C:/Program Files/Tutorial/include/TutorialConfig.h
-- Installing: C:/Program Files/Tutorial/bin/libMathFunctions.a
-- Installing: C:/Program Files/Tutorial/include/MathFunctions.h
安裝結果:
C:/Program Files/Tutorial>tree /f
C:.
├─bin
│ libMathFunctions.a
│ Tutorial.exe
│
└─include
MathFunctions.h
TutorialConfig.h
執行make test:
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build>make test
Running tests...
Test project D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build
Start 1: TutorialRuns
1/5 Test #1: TutorialRuns ..................... Passed 0.01 sec
Start 2: TutorialComp25
2/5 Test #2: TutorialComp25 ................... Passed 0.01 sec
Start 3: TutorialNegative
3/5 Test #3: TutorialNegative ................. Passed 0.01 sec
Start 4: TutorialSmall
4/5 Test #4: TutorialSmall .................... Passed 0.00 sec
Start 5: TutorialUsage
5/5 Test #5: TutorialUsage .................... Passed 0.00 sec
修改一個測試用例,讓它不過:
修改頂層CMakeLists.txt,然後重新Configure和Generate,然後make test即可看到結果。
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build>make test
Running tests...
Test project D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build
Start 1: TutorialRuns
1/5 Test #1: TutorialRuns ..................... Passed 0.01 sec
Start 2: TutorialComp25
2/5 Test #2: TutorialComp25 ...................***Failed Required regular expre
ssion not found.Regex=[25 is 3
] 0.01 sec
Start 3: TutorialNegative
3/5 Test #3: TutorialNegative ................. Passed 0.01 sec
Start 4: TutorialSmall
4/5 Test #4: TutorialSmall .................... Passed 0.01 sec
Start 5: TutorialUsage
5/5 Test #5: TutorialUsage .................... Passed 0.01 sec
2 - TutorialComp25 (Failed)
Errors while running CTest
make: *** [test] Error 8
4、Step4
檢查方法:
# does this system provide the log and exp functions?
include (CheckFunctionExists.cmake)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)
(2)修改.in文件,定義宏。(修改TutorialConfig.h.in,cmake執行中會把宏定義爲合適的值,生成TurorialConfig.h,供編譯時使用)
// does the platform provide exp and log functions?
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP
#if defined (HAVE_LOG) && defined (HAVE_EXP)
result = exp(log(x)*0.5);
#else // otherwise use an iterative approach
5、Step5
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step5/build>make
Scanning dependencies of target MakeTable
[ 25%] Building CXX object MathFunctions/CMakeFiles/MakeTable.dir/MakeTable.cxx.
obj
Linking CXX executable MakeTable.exe
[ 25%] "Built target MakeTable"
[ 50%] Generating Table.h
'.' 不是內部或外部命令,也不是可運行的程序
或批處理文件。
make[2]: *** [MathFunctions/Table.h] Error 1
make[1]: *** [MathFunctions/CMakeFiles/MathFunctions.dir/all] Error 2
make: *** [all] Error 2
問題分析:
MakeTable: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 MakeTable
.PHONY : MakeTable
再看Makefile2文件,找到出錯時正在編譯的目標。
MathFunctions/CMakeFiles/MakeTable.dir/all:
$(MAKE) -f MathFunctions/CMakeFiles/MakeTable.dir/build.make MathFunctions/CMakeFiles/MakeTable.dir/depend
$(MAKE) -f MathFunctions/CMakeFiles/MakeTable.dir/build.make MathFunctions/CMakeFiles/MakeTable.dir/build
$(CMAKE_COMMAND) -E cmake_progress_report D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step5/build/CMakeFiles 1
@echo "Built target MakeTable"
.PHONY : MathFunctions/CMakeFiles/MakeTable.dir/all
Make規則的執行順序是按照命令的先後順序:
echo "First line."
echo "Second line."
那麼make結果:
D:/Users/Desktop>make
echo "First line."
First line.
echo "Second line."
Second line.
6、Step6
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step6/build>make package
[ 50%] "Built target MathFunctions"
[100%] "Built target Tutorial"
Run CPack packaging tool...
CPack Error: Cannot find NSIS registry value. This is usually caused by NSIS not
being installed. Please install NSIS from http://nsis.sourceforge.net
CPack Error: Cannot initialize the generator NSIS
make: *** [package] Error 1
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step6/build>make package
[ 50%] "Built target MathFunctions"
[100%] "Built target Tutorial"
Run CPack packaging tool...
CPack: Create package using NSIS
CPack: Install projects
CPack: - Run preinstall target for: Tutorial
CPack: - Install project: Tutorial
CPack: Compress package
CPack: Finalize package
CPack: Package D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step6/build/T
utorial-1.0.1-win32.exe generated.
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step6/build>make package_sou
rce
Run CPack packaging tool for source...
CPack Error: Cannot find a suitable ZIP program
CPack Error: Cannot initialize the generator ZIP
make: *** [package_source] Error 1
ental
Site: JELLY-PC2
Build name: Win32-make
Create new tag: 20100521-1833 - Experimental
Configure project
Each . represents 1024 bytes of output
. Size of output: 0K
Build project
Each symbol represents 1024 bytes of output.
'!' represents an error and '*' a warning.
. Size of output: 0K
0 Compiler errors
0 Compiler warnings
Test project D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step7/build
Start 1: TutorialRuns
1/9 Test #1: TutorialRuns ..................... Passed 0.01 sec
Start 2: TutorialUsage
2/9 Test #2: TutorialUsage .................... Passed 0.01 sec
Start 3: TutorialComp4
3/9 Test #3: TutorialComp4 .................... Passed 0.01 sec
Start 4: TutorialComp9
4/9 Test #4: TutorialComp9 .................... Passed 0.01 sec
Start 5: TutorialComp5
5/9 Test #5: TutorialComp5 .................... Passed 0.01 sec
Start 6: TutorialComp7
6/9 Test #6: TutorialComp7 .................... Passed 0.01 sec
Start 7: TutorialComp25
7/9 Test #7: TutorialComp25 ................... Passed 0.01 sec
Start 8: TutorialComp-25
8/9 Test #8: TutorialComp-25 .................. Passed 0.01 sec
Start 9: TutorialComp0.0001
9/9 Test #9: TutorialComp0.0001 ............... Passed 0.01 sec
100% tests passed, 0 tests failed out of 9
Total Test time (real) = 0.19 sec
Performing coverage
Cannot find any coverage files. Ignoring Coverage request.
Submit files (using http)
Using HTTP submit method
Drop site:http://
Error when uploading file: D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutoria
l/Step7/build/Testing/20100521-1833/Build.xml
Error message was: couldn't connect to host
Problems when submitting via HTTP
Errors while running CTest