我正在 VS Code 中调试 C++ 代码,但它不会在断点处停止并可视化变量、监视和调用堆栈,而这是它应该做的。相反,它在调试控制台中打印此内容:
Breakpoint 1, 0x000000000040074a in main ()
[Inferior 1 (process 9445) exited normally]
The program '/home/hashir/x/a.out' has exited with code 0 (0x00000000)
这里是launch.json文件:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/hashir/x/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "/home/hashir/x/",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
使用
-g
标签以及 g++/clang++ 编译程序。
这个问题简直毁了我的一天。结果我要做的就是
Terminal
> Run Build Task
或 Ctrl+Shift
+ B
然后开始调试。
我发现如果你的源代码文件名包含空格,比如“Binary Search.cpp”,VSCode 将忽略断点,无论“stop at entry”配置如何。从我的源文件名称中删除空格是有效的,尽管它们的路径包含空格。例如“C++练习/BinarySearch.cpp”似乎是有效的。
在 GitHub 上打开的this问题中,建议非 ASCII 字符可能会导致此类问题。
默认的launch.json文件有这一行:
"program": "${workspaceFolder}/a.out",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
这将创建一个与当前文件同名的可执行文件。
更改tasks.json以创建a.out文件或更改launch.json文件,不使用当前文件的扩展名。
launch.json:
"program": "${workspaceFolder}/<stripped current file name>",
或
任务.json:
"-o",
"${fileDirname}/a.out"
环境变量似乎有问题,尝试从“VScode 的开发人员命令提示符”打开 vscode,然后在提示符下输入:code
就我而言,这是在单独的终端中使用 cmake 手动构建,然后使用 VSCode 中的 Docker 开发环境运行二进制文件时发生的。
删除构建目录并在开发环境中重新配置/重建解决了该问题。