[英]GDB can't debug running process using VS Code but can through command line
[英]Debug GNU using VS Code without GDB
在周末我通過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沒有內部調試器( 參見此處 )。 您需要使用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}
是${workspaceFolder}
的路徑變量,似乎指向C:\\\\School\\\\
,因此您可能需要修改"program"
的值以指向要調試的應用程序。 您還可以指定程序的絕對路徑。
另外,不要忘記使用debug-flags( -g
)編譯代碼,GDB需要這些代碼來逐步執行代碼。 例如: g++ -g main.cpp -o main.exe
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.