首页 > 解决方案 > 在 WSL 中使用 GDB 进行调试时的输入/输出

问题描述

我已经开始在 Windows 上使用 Visual Studio Code 并且我想调试以下文件

main.c(位于C:/Users/aibrakov/Projects/c目录):

#include <stdio.h>

int main(void) {
    int x = 42;  // add breakpoint here
    printf("Hello World!\n");
    getchar();
    return 0;
}

按照这个线程

我有

设置.json

{
    "window.zoomLevel": 0,
    "terminal.integrated.shell.windows": "c:\\windows\\sysnative\\bash.exe"
}

任务.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build project",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g", "main.c"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

启动.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "/mnt/c/Users/aibrakov/Projects/c/a.out",
            "stopAtEntry": false,
            "cwd": "/mnt/c/Users/aibrakov/Projects/c",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "pipeTransport": {
                "pipeCwd": "",
                "pipeProgram": "c:\\windows\\sysnative\\bash.exe",
                "pipeArgs": [
                    "-c"
                ],
                "debuggerPath": "/usr/bin/gdb"
            },
            "sourceFileMap": {
                "/mnt/c": "c:\\"
            }
        }
    ]
}

Ctrl使用++构建项目Shift成功B

调试有点工作:它在断点处停止并且DEBUG CONSOLE可以访问选项卡,但我没有得到输出(Hello World!在我的示例中)并且也无法发送getchar等待的字符。

有没有办法在我的程序运行的终端上工作?还是我错过了什么?

标签: visual-studio-codegdbwindows-subsystem-for-linux

解决方案


推荐阅读