VS code 调试易错点
- 调试运行源代码时,明明文件中有.exe文件,vs code 却报错,可能是源文件的命名或者源文件路径中含有中文名称。
- 配置.json文件可以参考网址
launch.json(需要把 "miDebuggerPath"改成自己电脑中gdb.exe文件所在路径)
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "C/C++",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
},
]
}
tasks.json文件
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
- 当调试运行程序时,输出框出现一闪而过的情况。我们希望黑色串口停留下,并显示程序输出结果。参考网址
方法一、在程序return 0前设置断点。
方法二、在 return 0前 加入system(“pause”) - 如图所示
只有把下面这个改成UTF-8,cout才能生效否则看不到cout输出结果,当然printf没影响。
直接点击UTF-8 选择save,修改类型 - 出现exception has occurred.segmentatoion fault.
原因在于没有给指针分配相应的存储空间
改正: