前記:VScode C/C++ 開發環境搭建及代碼運行參考另一篇文章:
Visual Studio Code -> VSCode 開發環境搭建 ---- C/C++ 開發環境搭建 及 代碼運行(code runner 插件)
VSCode 開發環境搭建 ---- C++ 代碼調試
平臺:Windows 10
IDE:Visual Studio Code :VSCode
功能環境:C++ 代碼調試環境
目錄
嚴文年 -- 記於蘇州
1. 開發平臺確認
確認開發平臺的系統信息:目前基於Windows 10 - 64位操作系統進行VSCode配置。
【1】. Windows 系統信息
【2】. VScode 版本信息
【3】. VScode 需安裝插件信息
2. 創建待調試 C++ 文件
【1】. 創建工作區
A. 創建代碼調試使用的工作區:
【2】. VScode加載工作區
A. 在創建的工作空間名稱上單擊鼠標右鍵,通過VScode打開工作空間:
B. VScode目錄顯示:
【3】. 代碼 C++ 編寫
A. 新建文件,輸入 C++ 代碼,保存文件到創建好的工作區。
附:C++ 待調試代碼
#include <iostream>
using namespace std;
int main()
{
int n, reverse=0, rem;
cout<<"Enter a number: ";
cin>>n;
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
cout<<"Reversed Number: "<<reverse<<endl;
return 0;
}
3. 配置 C++ 調試環境
【1】. launch.json文件配置
附:launch.json配置文件
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C++ debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\SoftWare\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "爲 gdb 啓用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
【2】. tasks.json文件配置
附:tasks.json配置文件
{
// 有關 tasks.json 格式的文檔,請參見
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "D:\\SoftWare\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\SoftWare\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
4. C++ 代碼調試
5. 後記
1. 以上是使用VScode + code runner 調試 C++ 代碼的完整配置過程。
2. 關於VScode + code runner 對同一工作區的多個文件(.h,.cpp) 代碼進行debug的配置:
見另一篇文章:Visual Studio Code -> VSCode 開發環境搭建 ---- 多個C++文件鏈接調試(code runner 插件)。