Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
通常會發現有許教學都是在使用CMake2.x來教學,不過這篇是使用CMake3.x來學習,兩者差異差在哪呢?
基本上一樣是基於3.x是2.x的修正進化版。
-
CMake 3.x版本引入了許多新的語法和功能改進,使得CMake文件的編寫更簡單、更直觀,並提供更多的靈活性。
-
CMake 3.x引入了目標屬性系統,這是一個重大的變化,可以通過目標屬性更好地管理編譯和連接過程。
-
CMake 3.x提供了更強大的自動化測試功能,包括CTest(測試框架)和CPack(軟體打包工具)的改進。
-
CMake 3.x更好地支援新的編譯器和工具鏈,以適應不斷變化的編程環境。
-
CMake 3.x包括對構建過程的性能和優化方面的改進,以提高構建速度和效率。
如何下載(linux/macOS以HomeBrew為例)
CMake應用
主要有善於build ,test和打包package software ,不依賴於任何平台或編譯器,使用CMakeLists.txt來編譯建立makefile & workspaces ,可以產生 靜態函式庫.a & 共享函式庫.so,以及連結OpenCV & Point Cloud Library
CMake 是一個自動編譯,測試和 packaging 的系統。一個尋常的 CMake workflow 如下:
- 在專案資料夾下建立 CMakeLists.txt 檔案
這一個步驟是在告訴CMake要尋找這個檔案,如果檔案名稱錯誤或者大小寫錯誤都會造成在編譯的過程產生錯誤。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 建立一個資料夾build。
此時會發現這個資料夾是空的

- 呼叫命令 cmake <CMakeLists.txt 所在的資料夾>。
這一步驟 cmake 會走訪整個專案資料夾,並讀取在子資料夾的 CMakeLists.txt。完成後會自動產生幾個重要的資料夾和檔案,部分列舉如下:
- CMakeCache.txt: 這會紀錄 configuration 的結果
- CMakeFile 資料夾
- cmake_install.cmake: 編譯完成的安裝檔
- Makefile
最後還有一個 <project name>的資料夾
在build資料夾當中使用這個指令會出現以下情況,看到Build files have been written to: 就代表有找到檔案位置
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 呼叫命令 make <Makefile > 進行編譯。
這一個步驟是代表前置作業都已經做好了
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
最後看到1 warning generated.[100%] Linking CXX executable main[100%] Built target main就代表編譯成功
最後你會看到以下架構,說明在build資料夾當中製作出了什麼東西:)
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
基本流程
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
CMakeLists.txt如何撰寫
講到這邊你可能覺得CMake好像很簡單,不過他值得學習的地方還有如何撰寫CMakeLists.txt,那廢話不多說,馬上來學習如簡單撰寫出常用的CMakeLists.txt 
以下列出常用的代表:
- 設定最低CMake版本要求:
- 設定專案名稱:
- 添加標頭檔 :
- 將源文件添加至專案中:
- 設定編譯選項:
- 連結外部庫:
最簡單又常用:
- 條件編譯:
- 安裝目標文件和標頭文件:
- 定義命令:
當然還有許多語法,不過這篇主要說明常用的指令以及自己有涉略到的語法
關於複雜的工程文件…Makefile如何看懂?如何寫?
- Makefile的主體 - Target
待更新…
下一章–> Git 版本控制
©Copyright © 2023 by CHUNG HONG HAO. All Rights Reserved.