# 怎么安裝VSCode和MinGW GCC編譯器
## 前言
在Windows平臺上進行C/C++開發時,Visual Studio Code(簡稱VSCode)配合MinGW GCC編譯器是一個輕量級且高效的選擇。本文將詳細介紹從零開始配置開發環境的完整流程,包括軟件下載、安裝配置、環境變量設置以及基礎代碼測試,幫助初學者快速搭建開發環境。
---
## 第一部分:安裝Visual Studio Code
### 1.1 下載VSCode
訪問VSCode官方網站:[https://code.visualstudio.com/](https://code.visualstudio.com/)
點擊"Download for Windows"按鈕下載安裝包(根據系統選擇32位或64位版本)。

### 1.2 安裝步驟
1. 雙擊下載的`.exe`安裝文件
2. 同意用戶協議,點擊"Next"
3. 選擇安裝路徑(默認`C:\Users\<用戶名>\AppData\Local\Programs\Microsoft VS Code`)
4. 在附加任務界面建議勾選:
- 創建桌面快捷方式
- 將"通過Code打開"操作添加到文件資源管理器上下文菜單
5. 點擊"Install"開始安裝
### 1.3 初次配置
安裝完成后首次啟動VSCode:
1. 選擇界面語言(推薦英文或中文簡體)
2. 安裝必要擴展:
- Chinese (Simplified) Language Pack(中文語言包)
- C/C++(Microsoft官方擴展)
- Code Runner(一鍵運行代碼)

---
## 第二部分:安裝MinGW GCC編譯器
### 2.1 MinGW簡介
MinGW(Minimalist GNU for Windows)是GNU工具鏈的Windows移植版本,包含:
- GCC編譯器套件(gcc/g++/gdb等)
- Windows API頭文件
- GNU Binutils工具集
### 2.2 下載MinGW
推薦使用MSYS2提供的MinGW-w64:
1. 訪問MSYS2官網:[https://www.msys2.org/](https://www.msys2.org/)
2. 下載對應系統版本的安裝包
### 2.3 安裝流程
1. 運行安裝程序,選擇安裝目錄(如`C:\msys64`)
2. 完成安裝后,從開始菜單啟動"MSYS2 MinGW 64-bit"
3. 在終端中更新軟件包數據庫:
```bash
pacman -Syu
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
C:\msys64\mingw64\bin
)
gcc --version
應顯示類似以下信息:
gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
C:\projects\hello_world
).vscode
子文件夾c_cpp_properties.json
文件:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/msys64/mingw64/include/**"
],
"defines": ["_DEBUG", "UNICODE"],
"compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
創建.vscode/tasks.json
:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with GCC",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
創建.vscode/launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug GCC",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
新建hello.c
文件:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Ctrl+Shift+B
執行構建任務F5
啟動調試終端應顯示:
Hello, World!
gcc --version
launch.json
中的gdb路徑正確-g
參數)在tasks.json
中添加編譯參數:
"args": [
"-fexec-charset=GBK",
...
]
通過以上步驟,您已成功搭建了基于VSCode和MinGW GCC的C/C++開發環境。這個輕量級組合既保留了Linux開發體驗,又能充分利用Windows系統的便利性。建議進一步探索: - VSCode的代碼重構功能 - GCC優化編譯選項 - GDB調試技巧
如需更專業的開發環境,可考慮: - CLion(商業IDE) - Visual Studio(微軟全家桶) - WSL2 + Linux原生GCC
注意:本文基于Windows 10/11系統編寫,其他系統可能需要調整路徑格式和安裝方式。 “`
(注:實際字符數約2800字,圖片鏈接為示例需替換為有效地址)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。