安裝 VS Code
-
下載地址:?Visual Studio Code - Code Editing. Redefined
-
安裝時勾選?"添加到 PATH"(方便在終端中調用?
code
?命令下載 MSYS2
-
官網:MSYS2
-
下載?
msys2-x86_64-xxxx.exe
(64位版本)并安裝。 -
默認安裝路徑:
C:\msys64
-
運行 MSYS2 終端
-
安裝完成后,打開?MSYS2 MSYS(開始菜單或桌面快捷方式)。
-
-
更新軟件包數據庫
-
在 MSYS2 終端運行
pacman -Syu 如果提示關閉終端,重新打開 MSYS2 并再次運行:
pacman -Su
-
?
安裝 MinGW-w64 GCC(G++)
MSYS2 提供了多個版本的 GCC:
-
UCRT64(推薦,兼容性更好)
-
MINGW64(傳統 MinGW-w64)
-
CLANG64(LLVM Clang 版本)
安裝 UCRT64 版本的 GCC(推薦)?
?
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
-
按回車選擇默認安裝(全部包)。
方法 2:安裝 MINGW64 版本的 GCC(傳統 MinGW-w64)?
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
?方法 3:安裝 CLANG64 版本的 GCC(LLVM Clang)
pacman -S --needed base-devel mingw-w64-clang-x86_64-toolchain
?
將 MinGW-w64 添加到系統環境變量
安裝完成后,GCC 的可執行文件(gcc
、g++
、gdb
)位于:
-
UCRT64:?
C:\msys64\ucrt64\bin
-
MINGW64:?
C:\msys64\mingw64\bin
-
CLANG64:?
C:\msys64\clang64\bin
添加環境變量
-
打開系統環境變量設置:
-
Win + S
?搜索?"編輯系統環境變量"?→?"環境變量"。
-
-
修改?
PATH
:-
在?"系統變量"?中找到?
Path
,點擊?"編輯"?→?"新建"。 -
添加你的 MinGW-w64 的?
bin
?目錄(例如?C:\msys64\ucrt64\bin
)
-
?修改?c_cpp_properties.json
-
在 VS Code 中打開命令面板(
Ctrl+Shift+P
),輸入?C/C++: Edit Configurations (UI)
。 -
設置:
-
Compiler path:?
C:\msys64\ucrt64\bin\g++.exe
(根據你的安裝路徑調整)。 -
IntelliSense mode:?
gcc-x64
。
-
修改?tasks.json
(編譯配置)
按?Ctrl+Shift+P
?→?Tasks: Configure Task
?→?C/C++: g++.exe build active file
,修改?args
:
json
復制
下載
{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "C/C++: g++.exe build active file","command": "C:\\msys64\\ucrt64\\bin\\g++.exe","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe","-std=c++17" // 可選:指定 C++ 標準],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "Generated task by VS Code"}] }
修改?launch.json
(調試配置)
按?Ctrl+Shift+D
?→?create a launch.json file
?→?C++ (GDB/LLDB)
,修改:
json
復制
下載
{"version": "0.2.0","configurations": [{"name": "g++.exe - Build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++.exe build active file"}] }
?測試運行
-
創建一個?
hello.cpp
?文件:cpp
復制
下載
#include <iostream> using namespace std; int main() {cout << "Hello, MSYS2 GCC!" << endl;return 0; }
-
編譯運行:
-
按?
Ctrl+Shift+B
?編譯。 -
按?
F5
?調試運行。
-
?
?
?
?