源**見
cmake 中支援變數來控制構建過程。乙個變數可通過$
取得其值,在 if 語句裡面,直接使用變數名而不需要取值。
通過 set 指令能顯式地自定義變數,有一些指令會隱式地建立變數,如 project 指令,會同時建立_source_dir
和_binary_dir
變數。
常用的一些變數 ::
project_name 專案名
cmake_binary_dir project_binary_dir _binary_dir 構建目錄
cmake_source_dir project_source_dir _source_dir 工程根目錄
cmake_current_source_dir 當前 cmakelists.txt 所在的目錄
cmake_currrent_binary_dir 當前 target 的構建目錄
cmake_current_list_file 當前 cmakelists.txt 完整路徑
cmake_current_list_line 當前執行的行號
cmake_module_path 加入 cmake 的擴充套件模組路徑
executable_output_path 可執行目標檔案輸出路徑
library_output_path 庫目標檔案輸出路徑
在 cmake 中呼叫 shell 環境變數用$env
,通過set(env 值)
設定。
跟系統相關的一些變數::
cmake_major_version,cmake 主版本號,比如 2.4.6 中的 2
cmake_minor_version,cmake 次版本號,比如 2.4.6 中的 4
cmake_patch_version,cmake 補丁等級,比如 2.4.6 中的 6
cmake_system,系統名稱,比如 linux-2.6.22
cmake_system_name,不包含版本的系統名,比如 linux
cmake_system_version,系統版本,比如 2.6.22
cmake_system_processor,處理器名稱,比如 i686.
unix,在所有的類 unix 平台為 true,包括 os x 和 cygwin
win32,在所有的 win32 平台為 true,包括 cygwin
以下是常用的指令
add_definitions 新增乙個 define 標誌
add_dependencies 定義依賴關係,add_dependencies(target depends-on-target;depends-on-target)
aux_source_directory 自動找到目錄下的原始檔,儲存到變數 aux_source_directory(dir variable)
cmake_minimum_required cmake 最小版本依賴
exec_program 執行乙個命令
file 檔案操作
include 載入 cmakelists.txt 或 cmake 模組,include 後直接執行
install 安裝
find_* 系列指令,包含 file library path program package 等
message 輸出訊息
if else endif 分支指令
分支指令經常用到,結構為 ::
if(expression)
# then section.
command1(args ...)command2(args ...)
...else(expression)
# else section.
command1(args ...)
command2(args ...)
...endif(expression)
表示式不為空、0、n、no、off、false、notfound 等,表示式為真。表示式可用 not and or existsis_newer_than
is_directory
matches 等進行比較。
對數字進行比較時,用 less greater equal 等。
對字串比較時,用 strless strgreater strequal 等。
cmake 中使用 while 和 foreach 迴圈指令。
while(condition)
command1(args ...)
command2(args ...)
...endwhile(condition)
foreach(loop_var arg1 arg2 ...)
command1(args ...)
command2(args ...)
...endforeach(loop_var)
foreach 可與 range 指令結合使用(有點像 python) ::
foreach(var range total)
...endforeach()
到這裡,已經基本能夠使用 cmake 來管理專案的構建了,更加複雜的用法需要學習 cmake 的模組寫法.cmake
檔案,以及熟悉 cmake 在不同平台上的工具,像 ccmake、 cmake-qt-gui 等。 CMake常用變數
cmake變數 cmake共用七種變數,如下所示 目錄 1 提供資訊的變數。2 控制變數。3 描述系統的變數。4 控制構建過程的變數。5 語言變數。6 ctest變數。7 cpack變數。first 提供資訊的變數 cmake binary dir cmake source dir cmake cu...
CMake學習筆記
cmake是乙個跨平台的安裝編譯工具,能夠生成各種各樣的makefile或者project檔案。cmake並不直接構建出最終的軟體,而是產生標準的構建檔案 即工程檔案,如unix下的makefile或windows下的c project檔案 然後構建者就可以使用平台的ide環境進行常規構建了。在wi...
CMAKE學習筆記
方法1 cmake中有兩個變數用於指定輸出檔案的位置,通過設定這兩個變數executable output path和library output path的值指定exe檔案和lib檔案放置的目錄,如 set executable output path set library output pat...