Try   HackMD

Windows VSCode MinGW C 語言開發攻略

VJ

參考微軟官方文件

安裝

  1. 下載並安裝 Windows VS Code
  2. 給 VS Code 安裝 C/C++延伸模組.
  3. 此頁指示,下載並安裝 MSYS2
    • 安裝完後於 MinGW 執行以下指令,安裝 Mingw-w64 時,-y 可跳過詢問輸入 [Y/n] 的步驟
      ​​​​​pacman -Sy -y mingw-w64-x86_64-gcc
      
    • [可選] 將 MinGW 語系改為中文

      逐行執行以下命令可將 MinGW 語系改為中文

      export LANG=zh_TW.UTF-8
      export LC_ALL="zh_TW.UTF-8"
      export LC_TIME="zh_TW.UTF-8"
      export LC_CTYPE="zh_TW.UTF-8"
      export LC_NUMERIC="zh_TW.UTF-8"
      export LC_COLLATE="zh_TW.UTF-8"
      export LC_MONETARY="zh_TW.UTF-8"
      export LC_MESSAGES="zh_TW.UTF-8"
      
    • 我以前以為跑完MSYS2官網的步驟VS Code就可以跑了,請把下面步驟做完
  4. 執行以下指令安裝 Mingw-w64 工具組,-y 可跳過詢問輸入 [Y/n] 的步驟
    ​​​pacman -Sy -y --needed base-devel mingw-w64-x86_64-toolchain
    
    • 接受預設(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 加入環境變數

  1. 按下 +R 後在執行視窗輸入下面指令後不要按確認
    ​​​rundll32 sysdm.cpl,EditEnvironmentVariables
    
  2. 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 →
    系統管理員身分執行
  3. 在「系統變數」找到變數Path後按「編輯」,再按「新增」
  4. 輸入預設安裝路徑: 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 變數清單中加上

"MINGW64": {
   "path": "C:\\msys64\\usr\\bin\\bash.exe",
   "label": "MSYS2",
   "args": [
       "--login",
       "-i"
   ],
   "env": {
       "MSYSTEM": "MINGW64",
       "CHERE_INVOKING": "1",
       "MSYS2_PATH_TYPE": "inherit"
   }
},

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"

"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

#include<stdio.h>
int main() {
    printf("Hello World!");
    return 0;
}

撰寫上面 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 →

注意每次執行完都會生出這些檔案:

  1. EXE 執行檔
  2. 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++ 程式了,欲知更多開發時用的功能如偵錯、環境參數設置等內容,請參考最上面的官方文件