Windows VSCode MinGW C 語言開發攻略
VJ
參考微軟官方文件
安裝
- 下載並安裝 Windows VS Code
- 給 VS Code 安裝 C/C++延伸模組.
- 按此頁指示,下載並安裝 MSYS2
- 安裝完後於 MinGW 執行以下指令,安裝 Mingw-w64 時,
-y
可跳過詢問輸入 [Y/n]
的步驟
-
[可選] 將 MinGW 語系改為中文
逐行執行以下命令可將 MinGW 語系改為中文
- 我以前以為跑完MSYS2官網的步驟VS Code就可以跑了,請把下面步驟做完
- 執行以下指令安裝 Mingw-w64 工具組,
-y
可跳過詢問輸入 [Y/n]
的步驟
- 接受預設(default)選項,預設就按 Enter ↲
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
將 Mingw64 加入環境變數
- 按下 +R 後在執行視窗輸入下面指令後不要按確認
- 按 Ctrl+Shift ⇧+Enter ↲ 以
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
系統管理員身分執行
- 在「系統變數」找到變數
Path
後按「編輯」,再按「新增」
- 輸入預設安裝路徑:
C:\msys64\mingw64\bin
後點擊空白處確認,若裝在其他地方記得改
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
新增終端機
下面步驟參考這個gist,但 VS Code 的設定變數更新了,以下步驟為寫這篇文章時成功的設定
2022,10,6 12:00 PM
在你的 VS Code 設定 JSON 檔中的 terminal.integrated.profiles.windows
變數清單中加上
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
完成
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
[可選] 預設 VS Code 終端機為 MINGW64
若希望將 MINGW64 設為預設終端機可以將 terminal.integrated.defaultProfile.windows
的值改為 "MINGW64"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
但是!如果要跑 C/C++ 請不要將 MINGW64 設為預設終端機,VS Code 在 ./.vscode
生成的指令不適用 MINGW64,請改用原本的 Powershell
Hello world
撰寫上面 C 語言文件,按下執行鍵
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選第一個
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
雖然會有一些編譯步驟和一長串指令,但還是執行完了
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
注意每次執行完都會生出這些檔案:
- EXE 執行檔
- VS Code 建置工作參數文件
.vscode/tasks.json
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
完成
好了現在你的 VS Code 能跑 C/C++ 程式了,欲知更多開發時用的功能如偵錯、環境參數設置等內容,請參考最上面的官方文件。