【问题标题】:Set breakpoints in C++ debugger (vs code)在 C++ 调试器中设置断点(与代码相比)
【发布时间】:2022-01-06 18:09:26
【问题描述】:

我遇到的问题是,当我运行“C++ 运行/调试活动文件”时,代码会执行,但不会在我设置的断点处停止。我不太了解调试器的工作原理,但我猜调试器没有听正确的进程?或者为什么会这样?

老实说,我一直在努力让我的所有依赖项都正确,并且在此过程中可能破坏了预期的功能,因此可能是我设置脚本的方式破坏了它的预期用途。

tasks.json 看起来像这样:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "g++",
            "args": [
                "main.cc",
                "-o",
                "main",
                "-std=c++17",
                "-I/usr/local/include/cairomm-1.16",
                "-I/usr/local/lib/cairomm-1.16/include",
                "-I/usr/include/cairo",
                "-I/usr/include/glib-2.0",
                "-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
                "-I/usr/include/pixman-1",
                "-I/usr/include/uuid",
                "-I/usr/include/freetype2",
                "-I/usr/include/libpng16",
                "-I/usr/include/sigc++-3.0",
                "-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
                "-L/usr/local/lib",
                "-lcairomm-1.16",
                "-lcairo",
                "-lsigc-3.0",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++-9 build active file",
            "command": "/usr/bin/g++-9",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-L/usr/local/lib",
                "-lcairomm-1.16",
                "-lcairo",
                "-lsigc-3.0"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}

launch.json 看起来像这样:

{
    "version": "0.0.0",
    "configurations": [
        {
            "name": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

c_cpp_properties.json 看起来像这样:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "g++",
            "args": [
                "main.cc",
                "-o",
                "main",
                "-std=c++17",
                "-I/usr/local/include/cairomm-1.16",
                "-I/usr/local/lib/cairomm-1.16/include",
                "-I/usr/include/cairo",
                "-I/usr/include/glib-2.0",
                "-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
                "-I/usr/include/pixman-1",
                "-I/usr/include/uuid",
                "-I/usr/include/freetype2",
                "-I/usr/include/libpng16",
                "-I/usr/include/sigc++-3.0",
                "-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
                "-L/usr/local/lib",
                "-lcairomm-1.16",
                "-lcairo",
                "-lsigc-3.0",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++-9 build active file",
            "command": "/usr/bin/g++-9",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-L/usr/local/lib",
                "-lcairomm-1.16",
                "-lcairo",
                "-lsigc-3.0"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}

我重做了它,这次变慢了。我没有启动文件,但是

这里是任务:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-I/usr/local/include/cairomm-1.16",
                "-I/usr/local/lib/cairomm-1.16/include",
                "-I/usr/include/cairo",
                "-I/usr/include/glib-2.0",
                "-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
                "-I/usr/include/pixman-1",
                "-I/usr/include/uuid",
                "-I/usr/include/freetype2",
                "-I/usr/include/libpng16",
                "-I/usr/include/sigc++-3.0",
                "-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
                "-L/usr/local/lib",
                "-lcairomm-1.16",
                "-lcairo",
                "-lsigc-3.0",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

这里是 c_cpp_properties

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/lib",
                "/usr/local/lib/cairomm-1.16/include",
                "/usr/local/include/cairomm-1.16",
                "/usr/include/sigc++-3.0",
                "/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
                "/usr/include/cairo",
                "/usr/include/freetype2"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++20",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

【问题讨论】:

  • @AlanBirtles 抱歉,已更新
  • 是的,这就是为什么我把它放在标签中,我应该把它放在标题中吗?
  • 有趣的是,微软如何设法获得像“用 g++ 构建一些 C++ 源代码”这样简单且易于理解的东西,如此复杂并与他们的平台 (VSC) 相关联,以至于我看着这个烂摊子就走了“不,不会打扰”。 :-D
  • @DevSolar 哈哈,是的,我不是粉丝,我正在完成一个新项目,并以更慢的速度将其组合在一起,以确保我理解每一步并希望抓住我打破的东西
  • @DevSolar 是的,vscode 更像是一个美化的文本编辑器而不是一个 ide,但是使用正确的插件,一个 cmake 项目无需额外配置即可工作。其他项目通过不尝试从 vs 代码内部构建而仅使用它来调用外部构建系统来简化

标签: c++ visual-studio-code


【解决方案1】:

我认为你的tasks.json中有很多不必要的代码,试试这个我可以在调试期间成功设置断点。

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${workspaceFolder}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\MinGW\\bin\\g++.exe"
        }
    ]
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • IDK 如果这是正确的答案,但我得到了它的工作,我很感激你的回复!我将发布我最终使用的问题
  • 实际上,我认为这是不对的,因为我需要很多选项才能使某些依赖项起作用,而且看起来您只选择了目录中的选项
  • 是的,我会在发布下一个答案之前尝试以更好的方式对其进行编辑,感谢您的建议!!
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 2013-07-16
  • 2023-03-08
相关资源
最近更新 更多