Try   HackMD

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的修正進化版。

  1. CMake 3.x版本引入了許多新的語法和功能改進,使得CMake文件的編寫更簡單、更直觀,並提供更多的靈活性。

  2. CMake 3.x引入了目標屬性系統,這是一個重大的變化,可以通過目標屬性更好地管理編譯和連接過程。

  3. CMake 3.x提供了更強大的自動化測試功能,包括CTest(測試框架)和CPack(軟體打包工具)的改進。

  4. CMake 3.x更好地支援新的編譯器和工具鏈,以適應不斷變化的編程環境。

  5. CMake 3.x包括對構建過程的性能和優化方面的改進,以提高構建速度和效率。

如何下載(linux/macOS以HomeBrew為例)

brew install cmake

CMake應用

主要有善於build ,test和打包package software ,不依賴於任何平台或編譯器,使用CMakeLists.txt來編譯建立makefile & workspaces ,可以產生 靜態函式庫.a & 共享函式庫.so,以及連結OpenCV & Point Cloud Library

CMake 是一個自動編譯,測試和 packaging 的系統。一個尋常的 CMake workflow 如下:

  1. 在專案資料夾下建立 CMakeLists.txt 檔案

這一個步驟是在告訴CMake要尋找這個檔案,如果檔案名稱錯誤或者大小寫錯誤都會造成在編譯的過程產生錯誤。

touch CMakeLists.txt

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. 建立一個資料夾build。
mkdir build

:no_entry_sign: 此時會發現這個資料夾是空的

  1. 呼叫命令 cmake <CMakeLists.txt 所在的資料夾>。

這一步驟 cmake 會走訪整個專案資料夾,並讀取在子資料夾的 CMakeLists.txt。完成後會自動產生幾個重要的資料夾和檔案,部分列舉如下:

  1. CMakeCache.txt: 這會紀錄 configuration 的結果
  2. CMakeFile 資料夾
  3. cmake_install.cmake: 編譯完成的安裝檔
  4. Makefile

最後還有一個 <project name>的資料夾

cmake ..

在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 →

  1. 呼叫命令 make <Makefile > 進行編譯。

這一個步驟是代表前置作業都已經做好了

make

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 :dart:

以下列出常用的代表:

  1. 設定最低CMake版本要求:
cmake_minimum_required(VERSION 3.10)
  1. 設定專案名稱:
project(MyProject)
  1. 添加標頭檔 :
add_library(Game name.cpp)
  1. 將源文件添加至專案中:
add_executable(my_executable main.cpp helper.cpp)
  1. 設定編譯選項:
target_compile_options(my_executable PRIVATE -Wall -Wextra)
  1. 連結外部庫:
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBRARY REQUIRED libname)
target_include_directories(my_executable PRIVATE ${LIBRARY_INCLUDE_DIRS})

最簡單又常用:

target_link_libraries(my_executable PRIVATE ${LIBRARY_LIBRARIES})
  1. 條件編譯:
option(ENABLE_FEATURE "Enable a feature" ON)

if(ENABLE_FEATURE)
    add_definitions(-DENABLE_FEATURE)
endif()
  1. 安裝目標文件和標頭文件:
install(TARGETS my_executable DESTINATION bin)
install(FILES helper.h DESTINATION include)
  1. 定義命令:
add_custom_target(my_custom_target COMMAND echo "Custom target")

當然還有許多語法,不過這篇主要說明常用的指令以及自己有涉略到的語法


關於複雜的工程文件Makefile如何看懂?如何寫?

  • Makefile的主體 - Target
    :hand_with_index_and_middle_fingers_crossed: 待更新

:ghost: 下一章> Git 版本控制

©Copyright © 2023 by CHUNG HONG HAO. All Rights Reserved.