VS Code在C++调试中忽略了断点。

15

我正在使用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
                }
            ]
        }
    ]
}

1
“stopAtEntry”: false 应该改为 “stopAtEntry”: true 吗? - ks1322
7个回答

36

使用g++/clang++编译程序时,同时加上-g标签。


谢谢,我也遇到了同样的问题。你的答案帮了我很多。 - KK2491
请问在哪里具体输入这个标签?谢谢。 - andreyb
@andreyb 在编译时加入该标志,例如运行 g++ file.cpp 命令。 - Oreo
在 tasks.json 文件中,在 tasks->args 下添加 -g 选项(例如参考我的回答)。 - undefined

3

这个问题让我一整天都很烦恼。结果发现我只需要做以下操作:在 终端 中选择 运行构建任务 或按下 Ctrl+Shift + B,然后开始调试。


太棒了!这也解决了我的问题!尽管在launch.json中有preLaunchTask标签。谢谢! - CppChase

1

我发现如果你的源代码文件名包含空格,比如 "Binary Search.cpp",无论 "stop at entry" 配置如何,VSCode 都会忽略断点。将源文件名称中的空格删除后,问题得到解决,尽管它们的路径包含空格。例如 "C++ Exercises/BinarySearch.cpp" 看起来是有效的。

GitHub 上开放的 这个问题中,有人建议非 ASCII 字符可能会导致这样的问题。


0
默认的 launch.json 文件有这一行:
"program": "${workspaceFolder}/a.out",
但是这将运行 a.out 来开始调试,如果你只配置了一个 cpp 文件的 tasks.json 文件,它将会有如下的行:

                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"  

这将创建一个与当前文件同名的可执行文件。
要么更改tasks.json以创建a.out文件,要么更改launch.json文件为当前文件的无扩展名。

launch.json:

"program": "${workspaceFolder}/<stripped current file name>", 

OR
tasks.json:

                "-o",
                "${fileDirname}/a.out"  

0
在我的情况下,这是在单独的终端中使用cmake手动构建,然后在VSCode中使用Docker开发环境运行二进制文件时发生的。
删除构建目录并在开发环境中重新配置/重建解决了这个问题。

0

看起来环境变量出了些问题,尝试从“VSCode开发人员命令提示符”中打开VSCode,然后在提示符上输入:code


0
在文件tasks.json中,在tasks->args下添加-g选项。
例如:
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\ProgramData\\msys64\\mingw64\\bin\\gcc.exe",
            "args": [
                "-g",
...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接