【发布时间】:2020-06-30 19:44:56
【问题描述】:
使用 GDB 在 VSCode 中调试 C 项目时,到 stdout 的输出(通过 printf())不会出现在调试控制台上。但是,当使用cppvsdebug 时,它可以工作。
这是我的 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": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"windows": {
"miDebuggerPath": "C:/raylib/mingw/bin/gdb.exe",
},
"osx": {
"MIMode": "lldb"
},
"linux": {
"miDebuggerPath": "/usr/bin/gdb",
},
"preLaunchTask": "build debug"
},
{
"name": "Run",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"program": "${workspaceFolder}/main",
"MIMode": "gdb",
"windows": {
"program": "${workspaceFolder}/main.exe",
"miDebuggerPath": "C:/raylib/mingw/bin/gdb.exe"
},
"osx": {
"MIMode": "lldb"
},
"linux": {
"miDebuggerPath": "/usr/bin/gdb"
},
"preLaunchTask": "build release",
}
]
}
这是为运行调试会话而执行的命令(根据终端窗口):
'c:\Users\...\.vscode\extensions\ms-vscode.cpptools-0.27.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-2dvbqvix.1cb' '--stdout=Microsoft-MIEngine-Out-1m5j1iy0.ufe' '--stderr=Microsoft-MIEngine-Error-oqyothgz.h5l' '--pid=Microsoft-MIEngine-Pid-hal3wx4b.uld' '--dbgExe=C:/raylib/mingw/bin/gdb.exe' '--interpreter=mi'
调试控制台显示 GDB 的“启动画面”信息,但不显示我的代码中 printf() 语句的任何输出:
=thread-group-added,id="i1"
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"
[New Thread 9488.0x54dc]
[New Thread 9488.0x577c]
[New Thread 9488.0xff4]
[New Thread 9488.0x4918]
当我从命令行(Git Bash)使用 GDB 运行 Exe 文件时,所有输出都正常显示。
将externalConsole 设置为 true 不起作用(一开始也不会打开外部控制台)。
我使用的是 Windows 10。
是否需要调整任何特定设置才能使其正常工作?
【问题讨论】:
-
也许你应该在代码中
fflush(stdout)来显示输出。特别是如果printf输出的末尾没有换行符。 -
最小的例子应该是
printf("Hello, World!\n");这样行吗? -
@Weather Vane 调用
fflush()不起作用。正如我所提到的,当直接从命令行使用 GDB 时,它也可以正常工作。所以我很确定它与 Visual Studio Code 有关。 -
@stark 不,它不起作用。
-
你可以尝试重新加载你的 gcc 编译器。检查 mingw-w64 版本并为您的 vscode 下载最新版本。
标签: c windows visual-studio-code gdb vscode-debugger