【问题标题】:GDB in Visual Studio Code doesn't show printf() output to stdoutVisual Studio Code 中的 GDB 不显示 printf() 输出到标准输出
【发布时间】: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


【解决方案1】:

"externalConsole" 设置为 true 为我解决了这个问题。

【讨论】:

    【解决方案2】:

    我最终解决了这个问题。在我的 launch.json 中,我将 filterStdout 设置为 true(我知道这是默认设置,但哈哈):https://code.visualstudio.com/docs/cpp/launch-json-reference#_filterstdout

    filterStdout 设置为false 会做同样的事情。所以我确定某处存在错误。我相信其目的是将其设置为 true 应该将标准输出输出到“调试控制台”选项卡中。

    现在,当我调用调用 printf 的方法时,我只需进入终端选项卡,瞧,我的调用在终端选项卡中!!!

    【讨论】:

      【解决方案3】:
      fflush(stdout);
      

      在您的 printf 语句对我有用之后。

      printf 写入输出流 stdout。 stdout 等待刷新(因此它可以显示在终端中)。它必须刷新才能显示,这通常通过换行符 (\n) 完成,因为 stdout 是行缓冲的。 (gh)

      【讨论】:

        【解决方案4】:

        但它会在换行符上输出.. 作为一种解决方法,添加一个 '\n' 来刷新。

        【讨论】:

          猜你喜欢
          • 2018-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-30
          • 1970-01-01
          • 2017-10-29
          相关资源
          最近更新 更多