1.创建一个文件夹,在vscode里打开,创建一个c.pp文件;
本地shell调试运行
2.要在编译完test.cpp后执行生成的可执行文件test.out:
在VS Code中按下 Ctrl + Shift + P 打开命令面板。
输入 Tasks: Configure Task 并选择 Create tasks.json file from template,然后选择 Others。在弹出的选项中,您可以选择 shell,然后在tasks.json文件中输入类似以下内容:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile and Run",
"type": "shell",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"-W",
"-Wall",
"-O2",
"-std=c++17"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"presentation": {
"reveal": "always",
"panel": "new"
}
}
]
}
3.点击左侧运行和调试
出现下图
4.如果要选择其他调试配置,点击小三角处没有配置框,选择C++(GBD/LLDB)
在弹框处选择 C/C++:g++.exe生成和调试活动文件,或者只需要继续在VS Code中按下 Ctrl + Shift + P 打开命令面板。
输入 Tasks: Configure Task 并选择C/C++:g++.exe生成活动文件,就会自动修改对应配置信息到tasks.json文件中:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile and Run",
"type": "shell",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"-W",
"-Wall",
"-O2",
"-std=c++17"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"presentation": {
"reveal": "always",
"panel": "new"
}
}
]
}
这时候左侧出现小三角,点击即可运行调试。
5.GBD命令调试
直接在终端编译: g++ test.cpp -g -o test
生成可执行文件后,开始调试:
gdb
file test
break xx //打断点
r //运行
info locals //查看局部变量名、值
step //单步调试
c //继续
info breakpoints //查看断点
clear breakpoints //清除所有断点
。。。。GDB调试命令大全请自由收集