開始 C++ 開發需兩大核心工具:文字編輯器 撰寫原始碼、C++ 編譯器 將原始碼轉換為機器碼可執行檔。
| 編輯器 | 平台 | 特色 |
|---|---|---|
| Visual Studio Code | 跨平台 | 輕量、擴充豐富 (C/C++、CMake Tools)、免費開源 |
| CLion | 跨平台 | JetBrains 專業 IDE、智慧補全、重構、需授權 |
| Visual Studio | Windows | 微軟旗艦 IDE、MSVC 編譯器、強大除錯、社群版免費 |
| Vim / Neovim | 跨平台 | 模式編輯、極致鍵盤操作、學習曲線陡峭 |
| Sublime Text | 跨平台 | 快速、美觀、套件生態、需授權 |
| Notepad++ | Windows | 輕量、免費、適合入門 |
原始碼檔案慣用副檔名:.cpp、.cc、.cxx、.c++、.C;標頭檔:.h、.hpp、.hxx。
| 編譯器 | 平台 | 特色 |
|---|---|---|
| GCC (g++) | Linux/macOS/Windows (MinGW/MSYS2/WSL) | GNU 編譯器套件、標準支援完整、免費開源、跨平台首選 |
| Clang (clang++) | 跨平台 | LLVM 專案、診斷訊息優異、模組化架構、Apple Xcode 預設 |
| MSVC (cl.exe) | Windows | 微軟編譯器、Windows SDK 整合、Visual Studio 內建 |
| Intel ICX | Linux/Windows | Intel 最佳化、高效能計算領域優勢 |
建議:學習/開源專案優先 GCC/Clang;Windows 原生開發可用 MSVC;生產環境常多編譯器驗證捕捉可攜性問題。
sudo apt update
sudo apt install build-essential gdb cmake # g++, make, 工具鏈
g++ --version # 驗證
sudo dnf install gcc-c++ make cmake gdb # 或 yum
xcode-select --install # 安裝 Command Line Tools (含 clang++)
# 或安裝完整 Xcode (App Store)
brew install gcc cmake # Homebrew 安裝 GCC 多版本
方案一:MSYS2 (推薦,原生 GCC 體驗)
# 1. 下載安裝 MSYS2 (https://www.msys2.org/)
# 2. 開啟 MSYS2 終端機 (UCRT64)
pacman -Suy
pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-gdb
# 3. 將 C:\msys64\ucrt64\bin 加入 PATH 環境變數
方案二:MinGW-w64 獨立安裝
# 1. 下載 mingw-w64-install.exe (SourceForge/GitHub)
# 2. 安裝時選擇 Architecture: x86_64, Threads: posix, Exception: seh
# 3. 將安裝目錄 bin 加入 PATH
方案三:Visual Studio Community
cl.exe。方案四:WSL 2 (Windows Subsystem for Linux)
wsl --install 安裝 Ubuntu,進入 Linux 環境用原生 GCC/Clang,VS Code 遠端開發無縫體驗。CMakeLists.txt 描述專案,生成 Makefile/Ninja/VS 專案。-G Ninja。set(CMAKE_EXPORT_COMPILE_COMMANDS ON)。
// test.cpp
#include
int main() { std::cout << "Hello C++ Environment!" << std::endl; }
g++ test.cpp -o test -std=c++17 -Wall -Wextra -pedantic
./test
# 輸出: Hello C++ Environment!
compile_commands.json 享受完整語意高亮、跳轉、重構。