【问题标题】:Debugging C using Visual Studio Code on Windows在 Windows 上使用 Visual Studio Code 调试 C
【发布时间】:2018-10-11 23:09:29
【问题描述】:

我正在尝试在 Windows 10 上使用 Visual Studio Code 调试 C 程序, 我安装了 C/C++ 扩展。

我的问题是,当我在我的工作区 (E:\Docs\c) 中创建 Source.c 时,编写一些代码然后按 F5,它会显示错误消息 launch: program 'E:\Docs\c\a.exe' does not exist,这意味着 VSCode 没有t 做编译的事情。

同时,当我转到控制台并键入 gcc source.c,这将在同一文件夹中创建 a.exe,然后再次按 F5 时,它开始没有问题,但每次我想都这样做运行代码很烦人。

那么,有没有办法从 VSCode 内部编译代码?

这是我的c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}",
                "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

这是launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

【问题讨论】:

    标签: c visual-studio-code


    【解决方案1】:

    我认为您应该像这样将带有构建任务标签的预启动任务添加到 launch.json

    "preLaunchTask": "build" // label of your build task
    

    这意味着你应该在你的 tasks.json 中有以下带有标签的任务 build 例如

        "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "gcc -g source.c"
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
    

    “-g”标志对于启用调试也很重要

    【讨论】:

    • 你好,我应该在哪里添加 "preLaunchTask": "build" // 你的构建任务的标签?
    • @Pierre-LouisDeschamps 将其添加到 launch.json 的配置中
    猜你喜欢
    • 1970-01-01
    • 2021-05-11
    • 2015-10-11
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2017-07-03
    相关资源
    最近更新 更多