Try   HackMD

VSCode C/C++ 環境設定

安裝

安裝 gcc / g++

gcc 和 g++ 是撰寫 C 或 C++ 程式時必要的編譯器, Windows 在這裡推薦使用 MSYS2 來安裝。

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 →
注意 —— 以下內容僅限於 Windows 系統。

  1. MSYS2 官網下載 MSYS2 安裝器。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  2. 開啟 MSYS2 安裝器。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  3. 設定 MSYS2 安裝路徑。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  4. 設定 MSYS2 開始選單圖示。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  5. 安裝完成後開啟 MSYS2。

  6. 輸入 pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain 安裝 C/C++ 開發工具鏈。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  7. 接著按 Enter 開始安裝工具。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  8. 將工具加入 PATH 環境變數中。

    1. 在搜尋中找到「編輯系統環境變數」。
      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →
    2. 點擊「環境變數」。
      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →
    3. 在「系統變數」部分,選擇「Path」並點擊編輯。
      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →
    4. 將 MSYS2 安裝位置新增到 PATH 中(預設位置:C:\msys64\ucrt64\bin
      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →
    5. 點擊確定
  9. 打開一個新的終端機視窗,然後輸入以下指令,確保工具皆已成功安裝:

    ​​​​gcc --version & g++ --version & gdb --version
    

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  10. 大功告成!

安裝編輯器

我們推薦使用 Microsoft Visual Studio Code 作為主要的程式碼編輯器,簡稱 VSCode。

安裝 VSCode 請到下載頁面 選取你的作業系統的版本進行安裝。

安裝延伸模組

開啟 VSCode 後點擊左側的延伸模組分頁,搜尋 C/C++ Extension Pack,找到後點擊安裝即可。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

其他推薦安裝的延伸模組

這裡有列出了一些非常推薦安裝的延伸模組,可提升程式碼編寫體驗:

VSCode 自動排版設定

在 VSCode 中,點擊左下角的齒輪圖示以打開設定(或使用 Ctrl + , 快捷鍵)。

寫程式碼的時候建議將「自動儲存」設為「onFocusChange(當焦點變更時)」。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

並將「在儲存時格式化檔案」打勾。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

這樣在儲存程式碼的時候就會自動排版了。

常見問題

按下執行後我該選哪個

選路徑有 msys64 的選項。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

launch: 'path' does not exist

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

這個錯誤最常見的成因是路徑中含有非 ASCII 字元。

請在執行前先確認檔案路徑中是否含有非 ASCII 字元,有的話把檔案重新命名即可。

undefined reference to 'WinMain'

把你的原始檔移到 OneDrive 以外的地方再執行一次

終端機無法輸入

執行程式後在 VSCode 的整合終端機無法輸入最常見的成因是 gdb 在安裝時沒有自動設定參數,改使用 MSYS 安裝 gcc / g++ 即可解決。

透過 MSYS2 安裝請詳見 #安裝 gcc / g++