Cmake使用筆記,Cmakelist編寫

2021-08-21 15:57:38 字數 3141 閱讀 1083

project([languages] [...])
將name存到project_name,同時設定變數

include_directories([after|before] [system] dir1 [dir2 ...])
指定標頭檔案的搜尋路徑,相當於指定gcc的-i引數 

include_directories($/hello) #增加hello為include目錄

target_link_libraries(... ... ...)
新增鏈結庫,相同於指定-l引數 target_link_libraries(demo hello) #將可執行檔案與hello連線成最終檔案demo

link_directories(directory1 directory2 ...)

動態鏈結庫或靜態鏈結庫的搜尋路徑,相當於gcc的-l引數 

link_directories($/hello)#增加hello為link目錄

add_library([static | shared | module]

[exclude_from_all]

[source1] [source2 ...])

add_library(hellohello.cxx) #將hello.cxx編譯成靜態庫如libhello.a

add_executable([win32] [macosx_bundle]

[exclude_from_all]

[source1] [source2 ...])

add_executable(hellodemo demo.cxx demo_b.cxx) 指定編譯,好像也可以新增.o檔案,將cxx編譯成可執行檔案

add_subdirectory(source_dir [binary_dir]

[exclude_from_all])

add_subdirectory(hello) #包含子目錄

aux_source_directory()
基本語法是:aux_source_directory(dir variable),作用是發現乙個目錄下所有的源**檔案並將列表儲存在乙個變數中,這個指令臨時被用來自動構建原始檔列表,因為目前cmake還不能自動發現新新增的原始檔。比如:

可以通過後面提到的for each指令來處理這個list。

cmake_minimum_required(version [...] [fatal_error])
比如:cmake_minimum_required(version 2.5 fatal_error) 

如果cmake版本小與2.5,則出現嚴重錯誤,整個過程中止。

cmake_binary_dir

project_binary_dir

< projectname >_binary_dir

這三個變數指代的內容是一致的,如果是in-source編譯,指得就是工程頂層目錄;如果是out-of-source編譯,指的是工程編譯發生的目錄。project_binary_dir跟其它指令稍有區別,目前可以認為它們是一致的。

cmake_source_dir

project_source_dir

< projectname >_source_dir

這三個變數指代的內容是一致的,不論採用何種編譯方式,都是工程頂層目錄。也就是在in-source編譯時,他跟cmake_binary_dir等變數一致。project_source_dir跟其它指令稍有區別,目前可以認為它們是一致的。 

(out-of-source build與in-source build相對,指是否在cmakelists.txt所在目錄進行編譯。)

cmake_current_source_dir

當前處理的cmakelists.txt所在的路徑,比如上面我們提到的src子目錄。

cmake_currrent_binary_dir

如果是in-source編譯,它跟cmake_current_source_dir一致;如果是out-of-source編譯,指的是target編譯目錄。使用add_subdirectory(src bin)可以更改這個變數的值。使用set(executable_output_path 《新路徑》)並不會對這個變數造成影響,它僅僅修改了最終目標檔案存放的路徑。

cmake_current_list_file

輸出呼叫這個變數的cmakelists.txt的完整路徑

cmake_current_list_line

輸出這個變數所在的行

cmake_module_path

這個變數用來定義自己的cmake模組所在的路徑。如果工程比較複雜,有可能會自己編寫一些cmake模組,這些cmake模組是隨工程發布的,為了讓cmake在處理cmakelists.txt時找到這些模組,你需要通過set指令將cmake模組路徑設定一下。比如set(cmake_module_path,$/cmake)

這時候就可以通過include指令來呼叫自己的模組了。

executable_output_path

新定義最終結果的存放目錄

library_output_path

新定義最終結果的存放目錄

project_name

返回通過project指令定義的專案名稱。

cmake使用筆記

cmake方法 工程結構 project bin cmakelists.txt include test.h lib cmakelist.txt libtest.so src cmakelists.txt main.cpp test.cpp project目錄中 add subdirectory 目...

CMake使用筆記 帶ui檔案的Qt程式

set cmake include current dir on 包含編譯目錄 aux source directory dlg dlg cpp 查詢指定目錄下的所有原始檔,然後將結果存進指定變數名 file glob file h 載入資料夾下的所有檔案 file glob dlg file dl...

筆記 cmake 使用

1.新增路徑 add subdirectory replay 2.新增執行程式 add executable replay replay.cc 3.設定專案檔案路徑 set target properties replay properties folder replay 4.設定依賴庫 targe...