cmake常用語法參考

2021-10-25 06:30:29 字數 2229 閱讀 3318

《cmake常用語法參考》

利用cmake來構建c++工程是乙個非常方便的選擇,尤其是依賴的庫比較多的時候,或者工程比較大的時候都非常方便,這裡記錄一些cmake常用的語法,

key words:cmake

beijing, 2019

code:

agile pioneer  

2. cmake 執行命令

3. cmake include

定義變數

set

(env x86)

message

($) # 列印變數

# 定義巨集引數

add_definitions

(-d_glibcxx_use_cxx11_abi=0)

# 指定編譯結果存放路徑

set(cmake_runtime_output_directory .

./release)

條件控制語句
set

(env x86)

if(env strequal x86)

else()

endif

()

指定引用庫
#### 指定標頭檔案路徑

include_directories()

#### 指定包含所需庫檔案的資料夾

link_directories()

#### 指定所需庫檔案

link_libraries

()

引入openmp
find_package

(openmp)

if(openmp_found)

message

("********openmp found"

)set

(cmake_c_flags "$ $"

)set

(cmake_cxx_flags "$ $"

)set

(cmake_exe_linker_flags "$ $"

)endif

()

收集編譯檔案
#### 把src資料夾下的所以.cpp檔案賦值給src變數

file

(glob src "./src/*.cpp"

)

編譯語法
#### 編譯可執行檔案

add_executable

(***exe $

)#### 編譯動態庫檔案|靜態庫檔案

add_library

(***lib shared $

)add_library

(***lib static $

)#### 給編譯的檔案連線其依賴的庫檔案

target_link_libraries

(***exe $

)

install 語法
#### make install命令 移動動態庫和靜態庫檔案

install

(targets lib lib_static

library destination $

/lib

archive destination $

/lib

)#### make install命令 移動資料夾

install

(directory ***/*** destination yyy/yyy)

#### make install命令 移動檔案

install

(files ***.h ***.hpp destination yyy/yyy)

# 檢視cmake能否通過find_package找到openmp

cmake --help-module-list|

grep openmp

# 存放find***.cmake的預設路徑

/usr/share/cmake-3.10/modules

有時候會有很多重複的變數會在各個專案中使用,這種情況我們不必重複的定義這些變數,對我而言opencv幾乎用在所有的c++工程中,所以我不需要對每個工程的cmakelists都重複的定義opencv的標頭檔案和庫檔案,這就需要借助cmake語法中的include方法,有兩種方式:

2、指定cmake_module_path後就可以對該資料夾下的.cmake檔案通過檔名當作模組來引用了

CMake常用語法

cmake 最低版本號要求 cmake minimum required version 2.8 專案資訊 project demo 查詢當前目錄下的所有原始檔,並將名稱儲存到 dir srcs 變數 aux source directory dir srcs 新增標頭檔案搜尋目錄 project ...

cmake常用語法介紹

使用cmake編譯程式 cd path of cmakelist.txt cmake make make install 語法 1.link directories 查詢動態庫路徑,相當於makefile的 l選項,且該方法必須在add library和add executable之前 2.鏈結靜態...

mysql常用語法 MySQL常用語法

更新資料 update 表名 set 健名 更改的值 where 建 刪除資料 delete from 表名 where 索引 create index 索引名稱 on 表名 鍵名 建立試圖 create view 檢視名 鍵,鍵 as select matchno,abs won lost fro...