Try   HackMD

使用 VSCode 開發 C/C++ 程式

如果想要使用 VSCode(Visual Studio Code)開發 C/C++ 程式,只要進行簡單的設定就可以,請依照以下說明設定。

安裝 VSCode

首先安裝 VSCode:

  1. 請連至 https://code.visualstudio.com/download 下載並安裝 VScode。

  2. 安裝完成後執行 VSCode:

    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 →

    依照上述步驟按延伸模組鈕後鍵入 "chinese" 搜尋,在 Chinese(Traditional)Language Pack for Visual Studio Code 項目按 Install 安裝繁體中文延伸模組,將介面更改為繁體中文阪本:

    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 →

    Change Language and Restart 更改語言設定並重新啟動 VSCode。

  3. 安裝 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 →

    依照上述步驟按延伸模組鈕後鍵入 "C" 搜尋,在 C/C++ Extension Pack 項目右側按 Install 安裝:

    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 →

    這個延伸模組會連帶安裝 C/C++ 以及 C/C++ Theme 兩個模組。

  4. 安裝完即可關閉 VSCode。

安裝 C/C++ 編譯器

Windows 平台本身並沒有提供 C/C++ 編譯器,必須另行安裝。以下以免安裝只要解開壓縮檔的 WinLibs 為例,說明安裝步驟:

  1. 請連至 https://winlibs.com/#download-release

    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 →

    點選 64 位元 ZIP 版本下載後解壓縮:

    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 →

    本例解壓縮到 C:\ 下。

  2. 設定環境變數,以便能直接使用編譯器相關指令。請先按 🪟+R 開啟執行視窗:

    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 →

    鍵入 "control system" 按確定開啟系統視窗:

    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 →

    環境變數

    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 →

    使用者環境變數窗格往下捲動找到 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 →

    新增

    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 →

    輸入剛剛解壓縮的路徑,加上 "\bin",例如前面我解壓縮倒 C:\ 下,所以完整的路徑就是 "C:\mingw64\bin"。輸入後一路按確定完成設定。

  3. 測試安裝好的 C/C++ 編譯器,請重新開啟 VSCode:

    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 中開啟終端機窗格:

    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 →

    依照圖中分別執行 gcc --versiong++ --versiongdb --version 三個顯示個別工具版本的指令,如果都可以看到版本編號,表示安裝正確無誤。

測試編譯/執行 C/C++ 程式

  1. 使用 VSCode 編輯、編譯與執行 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 →

    執行『檔案/開啟資料夾』功能表命令:

    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 →

    瀏覽至你要撰寫程式的位置,建立一個新的資料夾,本例命名為 "cpp",請確認整體路徑上都沒有包含有中文的資料夾,按選擇資料夾開啟:

    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 →

    按左側檔案總管後再按工具列中的新增檔案鈕:

    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 →

    輸入檔名,本例為 "test.cpp":

    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 →

    輸入測試的 C++ 程式碼後按右上角向下的箭頭展開執行選項,選取 Run C/C++ File

    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 →

    首次編譯執行程式會出現組態清單,由於我們安裝的是 GCC 編譯器,這裡請選 C/C++:g++.exe

    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 →

    接著就會自動編譯程式碼,如果程式碼沒有語法錯誤,就可以成功編譯,並自動執行程式,應該會看到程式的執行結束碼為 0:

    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 →

    要實際看到程式執行結果,必須切換到終端機頁次,就會看到剛剛程式執行後輸出的 "Hello, world." 了。

這樣,就可以使用 VSCode 開發 C/C++ 了。