CMake使用教程

來源:互聯網
上載者:User

標籤:cut   use   dash   ted   text   exists   sdn   loading   使用步驟   

轉自 RichardXG 原文 CMake使用教程

CMake是一個比make更進階的編譯組態工具,它可以根據不同平台、不同的編譯器,產生相應的Makefile或者vcproj項目。
通過編寫CMakeLists.txt,可以控制產生的Makefile,從而控制編譯過程。CMake自動產生的Makefile不僅可以通過make命令構建項目產生目標檔案,還支援安裝(make install)、測試安裝的程式是否能正確執行(make test,或者ctest)、產生當前平台的安裝包(make package)、產生源碼包(make package_source)、產生Dashboard顯示資料並上傳等進階功能,只要在CMakeLists.txt中簡單配置,就可以完成很多複雜的功能,包括寫測試案例。
如果有嵌套目錄,子目錄下可以有自己的CMakeLists.txt。總之,CMake是一個非常強大的編譯自動設定工具,支援各種平台,KDE也是用它編譯的,感興趣的可以試用一下。 準備活動:(1)安裝cmake。:http://www.cmake.org/cmake/resources/software.html根據自己的需要下載相應的包即可,Windows下可以下載zip壓縮的綠色版本,還可以下載原始碼。(2)運行cmake的方法。(GUI、命令列)http://www.cmake.org/cmake/help/runningcmake.html
CMake使用步驟:
運行GUI的cmake介面:
cmake-2.8.1-win32-x86/bin/cmake-gui.exe
執行Configure:
運行之後,產生了如下檔案:

產生Makefile:
執行Generate之後產生如下檔案:
運行make進行編譯:
編譯完成後,在build目錄產生Tutorial.exe,運行Tutorial.exe 25就可以看到運行結果:
運行make install安裝程式:
運行make test進行測試:
 通過cmake tutorial學習CMake配置方法
http://www.cmake.org/cmake/help/cmake_tutorial.html可以在原始碼的Tests/Turorial目錄中找到這個手冊對應的代碼。1、Step1。(如果不知道如何使用cmake,以及如何使用編譯產生的Turorial.exe,可先看下前面“CMake使用步驟”的說明,它以Step4為例詳細介紹了使用過程,Step1的配置可能不夠完全,比如無法運行make install,無法運行make test,但可以參考。)
簡單的程式編譯。(1)運行GUI的cmake,指定要編譯的原始碼路徑和二進位檔案路徑(會自動建立)。(2)點擊Configure,配置成功後,再點擊Generate。配置需要選擇合適的編譯器,雖然我安裝了VC2008,但沒有配置成功;選擇Unix Makefiles,配置成功,它自動找到了DevC++下的gcc.exe等編譯器。
(3)在build3目錄執行make,就能夠編譯產生Turorial.exe了。
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step1/build3>make
Linking CXX executable Tutorial.exe
[100%] Built target Tutorial
可以運行一下Turorial.exe:D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step1/build3>Tutorial.exe
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
把子目錄編譯為庫,並且連結到最終的可執行檔。include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions) # 使得子目錄MathFunctions也能被編譯
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
產生makefile:
在GUI上點擊Configure,之後Generate還是灰色,再次點擊Configure,Generate就可以點擊了。編譯:在build目錄運行make,即可開始編譯,但是開始會報告sh.exe運行異常,應該是Tools下的UnxUtils的sh.exe與Win7不相容,發現有如下make檔案,估計是它導致的,於是把它重新命名,不使用UnxUtils下的make,就OK樂。D:/Tools/CMD/UnxUtils/usr/local/wbin/make.exe編譯過程:
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支援make install把程式安裝到系統指定目錄,並且運行一些測試檢查它是否能夠正常工作。a、安裝時使用的基礎目錄,由CMAKE_INSTALL_PREFIX指定。b、可以通過一個很簡單的用例檢查程式是否運行起來,沒有出現異常。(TurotialRuns只是一個用例名字)add_test (TutorialRuns Tutorial 25)c、macro方式進行多組資料的測試是非常簡潔方便的。#define a macro to simplify adding tests, then use it
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 a bunch of result based tests
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
100% tests passed, 0 tests failed out of 5
Total Test time (real) = 0.13 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
80% tests passed, 1 tests failed out of 5
Total Test time (real) = 0.13 sec
The following tests FAILED:
2 - TutorialComp25 (Failed)
Errors while running CTest
make: *** [test] Error 8

4、Step4檢查系統是否支援log和exp函數。(log和exp都是數學運算函數)
檢查方法:(1)頂層配置中使用CheckFunctionExists.cmake
# 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(3)在代碼中使用宏和log函數等。// if we have both log and exp then use them
#if defined (HAVE_LOG) && defined (HAVE_EXP)
result = exp(log(x)*0.5);
#else // otherwise use an iterative approachStep4的完整配置、產生Makefile、編譯、運行、安裝、測試過程,參見最前面的“CMake使用步驟”。
5、Step5動態產生源檔案,自動把源檔案編譯進系統中。make的時候出錯了:
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
問題分析:
首先看build/makefile檔案,關於MakeTable有如下規則:# Build rule for target.
MakeTable: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 MakeTable
.PHONY : MakeTable
再看Makefile2檔案,找到出錯時正在編譯的目標。# All Build rule for target.
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規則的執行順序是按照命令的先後順序:
如果Makefile內容如下:all:
echo "First line."
echo "Second line."
那麼make結果:
D:/Users/Desktop>make
echo "First line."
First line.
echo "Second line."
Second line.由此,Built target MakeTable輸出之後才失敗的。
6、Step6產生各種平台Windows/Ubuntu/etc.上的安裝包,包括二進位安裝包和源碼安裝包。可以把依賴的系統庫也打包。include (InstallRequiredSystemLibraries)使用CPack。由於Step1-7,後面一步的配置都包含了前面一步的配置,所以從Step5開始,就會遇到make的問題。為了編譯通過,可以修改MathFunctions目錄下的CMakeLists.txt和mysqrt.cxx,去掉所有對Table.h 的依賴。運行make package可以產生安裝包:第一次,因為沒有安裝nsis,提示如下問題:
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安裝NSIS之後,運行成功:
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.產生了如下的Windows安裝包檔案:
安裝完成後,還可以很方便的卸載它:
運行make package_source可以產生原始碼包。(我的電腦上提示找不到合適的zip程式)
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
7、Step7把結果發布到dashboard。
下面網址是一個公開的dashboard:
http://www.cdash.org/CDash/index.php?project=PublicDashboard
dashboard上顯示的項目名稱通過如下方式設定:
需要先把cmake/bin目錄加入path中,然後執行ctest -D Experimental。這裡遇到了一個錯誤。D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step7/build>ctest -D Experim
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
產生了如下一些檔案:
   

CMake使用教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.