CMakeLists檔案的編寫

2021-09-01 06:15:20 字數 1442 閱讀 1737

一、cmake使用慣例

1)在專案根目錄建立乙個build目錄:mkdir build && cd build

2)執行:cmake ../

3)確定生成makefile成功,執行make

二、乙個簡單的例子

假設當前的目錄為test

1)新建hello.cpp

2)新建cmakelists.txt

內容:add_executable(hello hello.cpp)

3)新建乙個build目錄並進入,執行cmake ../,可以看到build目錄下生成的makefile檔案;執行make,可以看到build目錄下生成的hello檔案。

4)執行./hello命令。

三、cmake常用命令和變數

add_executable:新增乙個可執行檔案構建目標

示例工程目錄結構:

cmakelists.txt

include / util.h

lib / libutil.a

src / main.cpp

1)給工程起個名字

project(hello)

2)讓cmake找到我的標頭檔案

include_directories(./include)

作用:把當前目錄下的include 資料夾加入到包含路徑

3)讓cmake找到我的原始檔

aux_source_directory(./src $)

作用:把當前路徑下src目錄下的所有原始檔路徑放到變數hello_src中

4)讓cmake找到我的庫檔案

link_directories($/lib)

5)告訴cmake我的構建目標

add_executable($ $)

6)告訴cmake我要鏈結到哪個庫

target_link_libraries($ util)

7)傳遞flags給c++編譯器

set(cmake_cxx_compiler      "clang++" )         # 顯示指定使用的c++編譯器 

set(cmake_cxx_flags   "-std=c++11")             # c++11 

set(cmake_cxx_flags   "-g")                     # 除錯資訊 

set(cmake_cxx_flags   "-wall")                  # 開啟所有警告 

set(cmake_cxx_flags_debug   "-o0" )             # 除錯包不優化 

set(cmake_cxx_flags_release "-o2 -dndebug " )   # release包優化

8)最後在檔案頭部新增cmake版本檢查

cmake_minimum_required ( version 3.0)

1、2、

CmakeLists檔案說明

cmakelists.txt 設定構建native library所需的最小cmake版本 cmake minimum required version 3.4.1 aux source directory studycpp dir srcs file glob allcpp.建立和命名乙個庫 ad...

CMakeLists檔案規劃學習

在學習他人 的過程中了解到一些cmakelists的使用技巧,所以進行乙個簡單總結 呼叫乙個包,就是常規的三個步驟 find package,include directions,target link libraries 包多的時候 太雜,所以我們需要把每個包對應的這些操作放在cmake資料夾下對...

CMakeLists 在實際專案中的編寫例項

最近在專案中經常需要編寫cmakelists,在此記錄一下正式專案中是如何編寫使用的,特此記錄便於日後查閱。project device authentication cmake minimum required version 3.5 include directories include src...