2025 打造頂級 Premium C++ 開發環境 | Visual Studio Code 完全指南
在 C++ 開發中,擁有一個高效、精準的開發環境至關重要。一個高效 Premium C++ 且舒適的開發環境能大幅提升生產力,還能幫助你減少錯誤、提升代碼品質,並讓開發過程更加順暢。隨著 2025 年的到來,我們將手把手帶你用 Visual Studio Code 打造專業級 Premium C++ 開發環境,整合現代化工具鏈與高效插件,以打造一個頂級的開發環境,並讓你輕鬆應對從基礎到高級的開發挑戰。

內容
為什麼選擇 VS Code 作為 Premium C++ 開發環境?
VS Code(Visual Studio Code)是一款輕量級且功能強大的程式碼編輯器,深受 C++ 開發者喜愛。它不僅免費開源,還能透過擴充功能整合 CMake、LLDB/GDB、Git,以適合現代 C++ 開發與專業級 IDE(如 Visual Studio、CLion)相媲美的體驗。本文將帶你一步步打造高效的 C++ 開發環境,讓你的開發流程更加順暢。
安裝 VS Code
VS Code 支援 Windows、macOS 和 Linux,請前往 官方網站 下載並安裝適合你系統的版本。
安裝 VS Code 必要插件
- C/C++(ms-vscode.cpptools)
- CMake Tools(ms-vscode.cmake-tools)
安裝 C++ 編譯器
VS Code 本身不內建 C++ 編譯器,因此我們需要安裝合適的編譯工具:
Windows:安裝 MinGW-w64(GCC)或 MSVC(Visual Studio Build Tools)。
macOS:下載完整 Xcode,並執行:
sudo xcodebuild -license accept
Linux:使用系統套件管理工具安裝 g++,例如:
# Ubuntu / Debian
sudo apt install g++
# Fedora
sudo dnf install gcc-c++
# Arch Linux
sudo pacman -S gcc
可透過以下指令確認是否成功安裝:
g++ --version
若是 macOS 下會有如下顯示 :
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
配置 VS Code 進行 C++ 開發
打開終端或命令提示字元 ( 範例以 macOS 為主 ),創建一個新的專案目錄,並進入該目錄:
mkdir my_cpp_project
cd my_cpp_project
使用 VS Code 打開該目錄:
code .
在專案目錄中創建以下文件和資料夾結構:
- 在 VS Code 中,點擊左側的「檔案總管」圖標。
- 右鍵點擊專案目錄,選擇「新增資料夾」,創建
src
和include
資料夾。 - 在
src
資料夾中創建main.cpp
和utils.cpp
文件。 - 在
include
資料夾中創建utils.h
文件。
my_cpp_project/
├── src/
│ ├── main.cpp
│ └── utils.cpp
├── include/
│ └── utils.h
├── CMakeLists.txt (optional)
└── .vscode/
├── tasks.json
└── launch.json
編寫程式碼
include/utils.h
#ifndef UTILS_H
#define UTILS_H
void printMessage(const char* message);
#endif // UTILS_H
src/utils.cpp
#include <iostream>
#include "../include/utils.h"
void printMessage(const char* message) {
std::cout << "Message: " << message << std::endl;
}
src/main.cpp
#include "utils.h"
int main() {
printMessage("Hello, C++ Project!");
return 0;
}
配置編譯任務
VS Code 使用 tasks.json
來定義編譯任務。
- 按下
,輸入「Tasks: Configure Task」,然後選擇「Create tasks.json file from template」。Ctrl (Cmd)
+Shift+P - 選擇「Others」作為模板。
- 修改生成的
tasks.json
文件,如下所示:
{
"version": "2.0.0",
"tasks": [
{
"label": "build", // Must match the "preLaunchTask" in launch.json
"type": "shell",
"command": "g++",
"args": [
"-g",
"${workspaceFolder}/src/*.cpp",
"-I${workspaceFolder}/include",
"-o",
"${workspaceFolder}/src/a.out"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Generated task by VS Code"
}
]
}
這個配置會將 src
目錄下的所有 .cpp
文件編譯成一個可執行文件,並輸出 a.out
到目錄中。
- 按下
or 按下Ctrl (Cmd)
+Shift+B
,輸入「Tasks: Run Build Task」 來執行編譯任務。Ctrl (Cmd)
+Shift+P
配置 Debug 環境
VS Code 使用 launch.json
來配置除錯環境。
- 點擊左側的「除錯」圖標,然後點擊「建立 launch.json 文件」。
- 選擇「C++ (GDB/LLDB)」。
- 修改生成的
launch.json
文件,如下所示:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug with LLDB", // Custom name
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/src/a.out", // Replace with your executable path
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb", // Must be set to "lldb"
"MIDebuggerPath": "/usr/bin/lldb", // Force specifying the LLDB path ( macOS )
"preLaunchTask": "build", // Must match the "label" in tasks.json
"logging": { // Optional: Enable logging for debugging issues
"exceptions": true,
"moduleLoad": false,
"engineLogging": false
}
}
]
}
這個配置會自動執行編譯任務並啟動除錯。
- 按下
F5
來啟動除錯。
使用 CMake 進行專案管理(進階)
如果你打算使用 CMake 來管理專案,可以按照以下步驟進行配置。
- 在專案根目錄下創建一個
CMakeLists.txt
文件,內容如下:
cmake_minimum_required(VERSION 3.10) # Minimum required CMake version
project(MyCppProject) # Define the project name
set(CMAKE_CXX_STANDARD 17) # Use C++17 (or change to C++20 if needed)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # Set the output directory for the executable to "bin/"
include_directories(include) # Include the "include" directory for header files
file(GLOB SOURCES "src/*.cpp") # Find all .cpp source files in the "src" directory
add_executable(my_program ${SOURCES}) # Create an executable named "my_program" from the source files
- 修改 tasks.json 文件中的 “${workspaceFolder}/src/a.out” 取代為 “${workspaceFolder}/build/bin/my_program”
- 修改 tasks.json 文件中的 “program”: “${workspaceFolder}/src/a.out” 取代為 “program”: “${workspaceFolder}/build/bin/my_program”
- 按下
,輸入「CMake: Configure」來配置專案。Ctrl (Cmd)
+Shift+P - 按下
,輸入「CMake: Build」來編譯專案。Ctrl (Cmd)
+Shift+P - 按下
F5
來啟動除錯。
結論
在2025年,打造一個強大的 C++ 開發環境在 VS Code 中變得如此簡單且高效。這個設置不僅讓開發過程更加流暢,也讓你有機會探索更先進的 Premium C++ 特性和工具。持續學習和更新你的開發環境,以確保你在這個快速發展的領域保持領先。