周末我通过 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/