在Window10安裝Visual Studio Code支援C++的編譯與執行。
Step1)安裝 Visual Studio Code
到網站https://code.visualstudio.com/下載安裝程式。
Step2)安裝 MinGW
到以下網址下載最新版的MinGW,筆者下載x86_64-15.2.0-release-posix-seh-ucrt-rt_v13-rev0.7z,使用7-zip解壓縮後,移動資料夾mingw64到指定磁碟機,筆者移動到C:\。
https://github.com/niXman/mingw-builds-binaries/releases
Step3)編輯系統環境變數
搜尋「編輯系統環境變數」,修改環境變數path
點選「環境變數」,「path->編輯」
新增路徑C:\mingw64\bin,點選「新增」,輸入路徑,最後點選「確定」
Step4)VS Code安裝C++擴充功能
點選擴充功能圖示,搜尋「C++」,點選C/C++ Extension Pack的Install進行安裝
Step5)開啟vscode資料夾
點選「File->Open Folder」,設定vscode資料夾
Step6)編輯C++編譯器環境
按下Ctrl + Shift+P,搜尋「 C/C++: Edit Configurations (UI)」
設定Compiler path為「 C:\mingw64\bin\g++.exe」
設定IntelliSense mode為「windows-gcc-x64」
此設定檔會自動產生在目前工作資料夾,新增資料夾.vscode,檔案c_cpp_properties.json
Step7)編輯一個簡單C++程式,點選「EXPLORER」內按下滑鼠右鍵,點選「New File」
建立hi.cpp檔案,撰寫簡單程式
Step8)進行第一次執行
點選「C/C++:g++.exe build ........」
在.vscode資料夾下,會自動產生task.json檔案
點選task.json進行修改,args下方紅色字的部分
"args": [
"-fdiagnostics-color=always",
"-g",
"${fileDirname}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
到此可以執行C++,在TERMINAL產生結果hi
Step9)設定gdb除錯功能,執行程式按鈕右邊的設定圖示,如下。
選擇(gdb) Launch會自動產生launch.json
在.vscode資料夾下會自動產生launch.json,編輯launch.json,設定 "miDebuggerPath"為 "C:\\mingw64\\bin\\gdb.exe",如下圖
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands":
Step10)設定中斷點,下拉選單選擇「Debug C/C++ File」,就可以除錯了
Step11)操作介面中文化
點選「Ctrl+Shift+p」,搜尋「language」選擇「Configure Display Language」
選擇「中文(繁體)」