在 C/C++ 開發中,管理專案的編譯流程是一個重要的課題。傳統的 Makefile 雖然能夠自動化編譯,但維護起來並不方便,特別是在跨平台開發時會遇到許多問題。
這時候,CMake 就成為了最佳解決方案!
本篇文章將介紹 CMake 是什麼、它的優勢,以及如何使用 CMake 來編譯 C/C++ 專案,讓你快速上手這個強大的工具!?
1. CMake 是什麼?為什麼要用 CMake?
CMake 是 跨平台的 C/C++ 專案編譯工具,它的主要用途是生成適合不同環境的 Makefile 或 IDE 專案檔案,並自動化編譯過程。
為什麼選擇 CMake?
✅ 跨平台支援 - 可用於 Windows、Linux、macOS
✅ 支援多種編譯器 - GCC、Clang、MSVC 皆可使用
✅ 生成多種構建系統 - 可產生 Makefile、Ninja、Visual Studio 專案等
✅ 容易管理大型專案 - 可拆分成多個子專案,提高可維護性
✅ 自動處理相依性 - CMake 內建 find_package() 可自動搜尋函式庫
簡單來說,CMake 讓 C/C++ 的專案編譯更靈活、可維護,並且適用於各種平台。
2. 安裝 CMake
在 Linux 安裝 CMake
sudo apt update
sudo apt install cmake
在 macOS 安裝 CMake
brew install cmake
在 Windows 安裝 CMake
- 下載 CMake:https://cmake.org/download/
- 安裝後,將 CMake 加入系統環境變數(勾選「Add CMake to the system PATH」)。
- 測試安裝:
cmake --version
3. 基本 CMake 專案
我們來建立一個簡單的 Hello World C++ 專案,並使用 CMake 來編譯它。
1. 建立專案結構
HelloCMake/
│── CMakeLists.txt
│── src/
│ ├── main.cpp
2. 撰寫 main.cpp
建立 src/main.cpp:
#include <iostream>
int main() {
std::cout << "Hello, CMake!" << std::endl;
return 0;
}
3. 撰寫 CMakeLists.txt
建立 CMakeLists.txt(專案的 CMake 配置檔案):
cmake_minimum_required(VERSION 3.10) # 設定最低 CMake 版本
project(HelloCMake) # 設定專案名稱
set(CMAKE_CXX_STANDARD 11) # 設定 C++ 標準版本
add_executable(hello src/main.cpp) # 指定要編譯的執行檔
4. 使用 CMake 編譯專案
1. 建立 build 目錄
CMake 的標準做法是將編譯檔案放在 build/ 目錄內,避免污染原始碼:
mkdir build
cd build
2. 生成 Makefile
cmake ..
這個指令會根據 CMakeLists.txt 產生對應的 Makefile。
3. 執行編譯
make
執行後,你會在 build/ 目錄內看到一個 hello 可執行檔。
4. 執行程式
./hello
輸出:
Hello, CMake!
5. CMake 常用指令
| 指令 | 說明 |
|---|---|
cmake . |
在當前目錄生成 Makefile |
cmake .. |
在 build/ 目錄內生成 Makefile |
cmake --build . |
使用 CMake 進行編譯(等同於 make) |
make |
使用 Makefile 進行編譯 |
cmake --install . |
安裝編譯後的執行檔 |
cmake --help |
查看 CMake 指令說明 |
6. CMake 進階功能
1. 設定 include 目錄
如果你的專案有額外的 .h 標頭檔,你可以使用 include_directories() 來指定:
include_directories(include)
2. 建立函式庫
如果你要建立可重複使用的函式庫,CMake 也能輕鬆管理。
專案結構
MyProject/
│── CMakeLists.txt
│── src/
│ ├── main.cpp
│ ├── math.cpp
│── include/
│ ├── math.h
math.h
#ifndef MATH_H
#define MATH_H
int add(int a, int b);
#endif
math.cpp
#include "math.h"
int add(int a, int b) {
return a + b;
}
main.cpp
#include <iostream>
#include "math.h"
int main() {
std::cout << "2 + 3 = " << add(2, 3) << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject)
set(CMAKE_CXX_STANDARD 11)
include_directories(include)
add_library(math_lib src/math.cpp)
add_executable(main src/main.cpp)
target_link_libraries(main math_lib)
編譯與執行
mkdir build
cd build
cmake ..
make
./main
輸出:
2 + 3 = 5
7. CMake 與 IDE 整合
CMake 支援多種 IDE,如 Visual Studio、CLion、Xcode,你可以用 CMake 生成對應的專案檔案:
Windows (Visual Studio)
cmake -G "Visual Studio 16 2019" ..
開啟 build/ 內的 .sln 檔案即可用 Visual Studio 編譯。
macOS (Xcode)
cmake -G "Xcode" ..
Linux / CLion
CLion 內建 CMake,只要開啟專案即可自動偵測 CMakeLists.txt 並編譯。
8. 結論
CMake 是 C/C++ 專案管理的標準工具,提供跨平台支援、自動相依性管理、支援 IDE 並且易於擴展。
CMake 的關鍵點: ✅ 跨平台支援(Linux、Windows、macOS)
✅ 產生 Makefile、Visual Studio、Xcode 等構建系統
✅ 易於管理大型專案(支援函式庫與子專案)
✅ 自動處理相依性
如果你正在學習 C/C++,現在就是最好的時機來學 CMake!?
你對 CMake 有哪些問題或心得?歡迎留言討論!?
評論