c++ - 在没有 GDB 的情况下使用 VS Code 调试 GNU

标签 c++ command-line gdb g++ visual-studio-code

周末我通过 MSYS2 bash 安装了一个 gcc。我在 VS 代码中设置它并让它正常工作。我什至让 GDB 工作(是的,我知道这是一个调试器)。但是,我的主要问题是,是否可以使用 VS 代码中的调试功能而不是 GDB 进行调试。按 F5 它会调出 launch.json 文件并给我 launch: program 'enter program name, for example c:\School\a.exe' does not exist 。经过一些研究,我看到你给它一个文件给 args 以允许它在调试器中运行。当我这样做时,我似乎既不能给它正确的文件,也不能让它整体工作。我还使用 a.exe 而不是 a.out。我不确定这是否有效果。

{
    // 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": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": ["C:\\School\\CSE340\\project2\\main.cpp"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

最佳答案

VS Code 没有内部调试器 (see here)。您需要使用 GDB 或 visual studio 调试器(如果有后者)。

在您的 launch.json 中,您需要修改条目:

"program":这是您要调试的程序的路径,即您编译的程序(可以是您的项目文件夹的相对路径)

“miDebuggerPath”:这是 GDB 的路径

“args”:这些是参数,你想传递给你的程序用于调试目的,即你可以留空

所以 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": "${workspaceFolder}\\CSE340\\project2\\main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", // Path where your gdb.exe is located
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

${workspaceFolder} 是您工作区的路径变量,似乎指向 C:\\School\\,因此您可能需要修改"program" 的值指向您要调试的应用程序。您还可以指定程序的绝对路径。

此外,不要忘记使用调试标志 (-g) 编译代码,GDB 需要这些标志才能单步执行代码。例如: g++ -g main.cpp -o main.exe

关于c++ - 在没有 GDB 的情况下使用 VS Code 调试 GNU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48751154/

相关文章:

c++ - 从 macports (macOS) 安装 gcc 8 的 gdb libstdc++ pretty-print 中的错误

C++:g++ 编译器:stdio 与虚函数的冲突?

c++ - g++ 不会在模板代码中发出 -Wsign-compare

c++ - 使用 symstore.exe 无法在 Windbg 或 Visual Studio 中为小型转储加载符号

c++ - 有没有办法在 Visual Studio 中捕获标准错误和标准输出?

command-line - 搜索字符串: Find and Grep

mysql - 为什么某些docker exec命令不能始终在批处理文件中工作?

不同类型的 C++ 模板运算符重载

gdb - clang 3.1 在 Ubuntu 12.04 上构建的二进制文件无法使用 gdb 进行调试

objective-c - 在gdb中调试objc时如何检查 'super'