使用无GDB的VS代码进行调试GNU

Debug GNU using VS Code without GDB

本文关键字:调试 GNU 代码 VS GDB      更新时间:2023-10-16

在周末,我通过MSYS2 Bash安装了GCC。我将其设置为VS代码,并使其正常工作。我什至让GDB工作(是的,我知道这是一个调试器(。但是,我的主要问题是,是否可以在VS代码中使用调试函数进行调试,而不是GDB。按F5,它会拔起启动。JSON文件并给我launch: program 'enter program name, for example c:Schoola.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代码没有内部调试器(请参阅此处(。您需要使用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