CMake編譯 雜記

2021-10-08 20:23:14 字數 4112 閱讀 4980

一、cmake常見的預置的巨集

project_name: 通過project指定的專案名稱

project

(demo)

project_source_dir: 工程的根目錄,上圖中的demo目錄

project_binary_dir: 執行cmake命令的目錄,一般是在build目錄,在此目錄執行cmake ..

cmake_current_source_dir: 當前cmakelists.txt檔案所在目錄

cmake_current_binary_dir: 編譯目錄,可使用add_subdirectory來修改此變數

cmake_include_path: 環境變數,非cmake變數

cmake_library_path: 環境變數

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

cmake_current_list_line: 輸出這個變數所在的行

cmake_module_path: 定義自己的cmake模組所在的路徑

set(cmake_module_path $

/cmake),然後可以用include命令來呼叫自己的模組

設定輸出

set(executable_output_path $

/bin) //設定編譯後可執行檔案輸出路徑

set(library_output_path $

/lib) //library_output_path

: 庫檔案輸出位置

常用系統資訊變數

cmake_major_version

: cmake的主版本號cmake version 3.11.2中的3

cmake_minor_version

: cmake的次版本號cmake version 3.11.2中的11

cmake_patch_version

: cmake的補丁等級cmake version 3.11.2中的2

在這裡插入描述

cmake_system

: 系統名稱,帶版本號

cmake_system_name

: 系統名稱,不帶版本號

cmake_system_version

: 系統版本號

cmake_system_processor

: 處理器名稱

編譯選項

build_shared_libs

: 預設的庫編譯方式(shared or static),預設為static,一般在add_library時直接指定編譯庫的型別

cmake_c_flags

: 設定c編譯選項

cmake_cxx_flags

: 設定c++編譯選項

cmake_cxx_flags_debug

: 設定編譯型別為debug時的編譯選項

cmake_cxx_flags_release

: 設定編譯型別為release時的編譯選項

cmake_cxx_compiler

: 設定c++編譯器

# 設定c++編譯器為g++

set(cmake_cxx_compiler "g++"

)# 設定標準庫版本為c++17 並開啟警告

set(cmake_cxx_flags "-std=c++17 -wall"

)# 設定debug模式下,不開啟優化,開啟除錯,生成更詳細的gdb除錯資訊

set(cmake_cxx_flags_debug "-o0 -g -ggdb"

)# 設定release模式下,開啟最高端優化

set(cmake_cxx_flags_release "-o3"

)

二、include_directories 和 target_include_directories的區別參考:

include_directories

(x/y):新增標頭檔案搜尋目錄, 對應的是cmakelists檔案裡所有的target,報錯include_subdirectory裡新增的目標,都會去x/y目錄下尋找標頭檔案

target_include_directories

(t x/y):新增標頭檔案搜尋目錄,對應的是cmakelists檔案裡某個特指的target t,

target_include_directories

() 支援使用 private, public,和inte***ce qualifiers來修飾

三、區分是否是android平台編譯

add_definitions

(-d android)

四、build_inte***ce

使用示例:

cmake構建乙個靜態鏈結庫的例項

五、遞迴新增標頭檔案搜尋目錄

# 定義函式,用於遞迴新增標頭檔案搜尋目錄

function

(include_sub_directories_recursively root_dir)

if(is_directory $

)# 當前路徑是乙個目錄嗎,是的話就加入到包含目錄

message

("include dir: " $

)include_directories($)

endif()

file

(glob all_sub relative $ $/*

)# 獲得當前目錄下的所有檔案,讓如all_sub列表中

foreach

(sub $)if

(is_directory $/$)

include_sub_directories_recursively

($/$

)# 對子目錄遞迴呼叫,包含

endif()

endforeach()

endfunction

()

六 glob 和 glob_recurse

#新增所有原始檔,可定義檔案格式。

#說明一下:glob_recurse

#file

(glob_recurse variable [relative path]

#[follow_symlinks]

[globbingexpressions]

...)

#glob_recurse 與glob類似,區別在於它會遍歷匹配目錄的所有檔案以及子目錄下面的檔案。

#對於屬於符號鏈結的子目錄,只有follow_symlinks指定一或者cmake策略cmp0009沒有設定為new時,才會遍歷這些目錄。

##glob的使用方法:

#file

(glob variable [relative path]

[globbingexpressions]

...)

##glob 會產生乙個由所有匹配globbing表示式的檔案組成的列表,並將其儲存到變數中。

#globbing 表示式與正規表示式類似,但更簡單。如果指定了relative 標記,

#返回的結果將是與指定的路徑相對的路徑構成的列表。 (通常不推薦使用glob命令來從原始碼樹中收集原始檔列表。

#原因是:如果cmakelists.txt檔案沒有改變,

#即便在該原始碼樹中新增或刪除檔案,產生的構建系統也不會知道何時該要求cmake重新產生構建檔案。

macro

(rw_module_source_all)

file

(glob_recurse rw_module_source_list .

"*.cpp"

"*.c"

"*.h"

"*.hpp"

"*.inl"

)aux_source_directory

(. rw_module_source_list)

endmacro

(rw_module_source_all)

核心編譯雜記

make 澄清 make oldconfig 裡面說到make oldconfig是將.config檔案進行乙個備份。在實際測試的過程中,發現可能不是簡單的備份.config檔案。當缺少構建工具的時候,比如bison,flex時,命令執行失敗。而且測試發現,make oldconfig會直接改變.c...

cmake交叉編譯

1 設定交叉編譯之前,必須在 cmakelist.txt 前面加上這樣一句,這樣 cmake 才會認為你是要交叉編譯 set cmake system namelinux 其中linux 是要編譯過去的平台,如果你是在 linux 下交叉編譯 window 的東西,就要寫成 windows 了。我是...

cmake編譯報錯

問題 cmake編譯報錯 cmake error the source.此處大概意思是乙個檔案位址does not match the 另外乙個檔案位址so used to generate cache.rerun cmake.經過觀察可以發現兩個檔案位址其實只有一點差異,所以 第一次嘗試 按照第二...