【问题标题】:Visual studio code debugger error : "Could not find the task 'gcc build active file'Visual Studio 代码调试器错误:“找不到任务‘gcc build active file’
【发布时间】:2020-03-25 03:59:01
【问题描述】:

我正在尝试使用 Ubuntu Linux 在 Visual Studio Code 中配置 C/C++ 工作区,但我不知道如何使调试器正常工作。我从互联网上复制了一个“tasks.json”文件,以便能够通过按 F5 来编译我的代码,但我认为它会导致调试器出现某种问题,因为每次我尝试进入调试模式时,都会出现错误“可能找不到任务“gcc build active file”弹出。 这是2个json: 任务.json

{
"version": "2.0.0",
"tasks": [
    {
        "label": "debug",
        "type": "shell",
        "command": "",
        "args": [
            "g++",
            "-g",
            "${relativeFile}",
            "-o",
            "a.exe"
        ]
    },
    {
        "label": "Compile and run",
        "type": "shell",
        "command": "",
        "args": [
            "g++",
            "-g",
            "${relativeFile}",
            "-o",
            "${fileBasenameNoExtension}.out",
            "&&",
            "clear",
            "&&",
            "./${fileBasenameNoExtension}.out"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": [
                "relative",
                "${workspaceRoot}"
            ],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    },
    {
        "type": "shell",
        "label": "g++ build active file",
        "command": "/bin/g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build"
    }
]

}

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": "enter program name, for example ${workspaceFolder}/a.out",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    },
    {
        "name": "gcc build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "gcc build active file",
        "miDebuggerPath": "/usr/bin/gdb"
    }
]

}

提前感谢您的帮助,我真的很无能为力。

【问题讨论】:

    标签: linux visual-studio-code vscode-debugger


    【解决方案1】:

    在您的task.json 文件中,没有任务被标记为“gcc 构建活动文件”,这是launch.json 文件中的preLaunchTask 所必需的。

    因此您可以更改任务的label 或更改preLaunchTask 的内容以使其匹配。

    只需将preLaunchTask的内容更改为“g++ build active file”即可。它会起作用的。

    【讨论】:

    • 为了清楚起见,我相信 Albert 指的是 tasks.json 文件
    • 它们在我的情况下都匹配,我仍然得到这个错误,我只需要重新启动 VS Code,更多信息在这里:stackoverflow.com/a/55107773/1689770
    【解决方案2】:

    您需要指定文件的路径和名称。当然,只有使用 g 标志编译二进制文件时才能进行调试(输出变得更重且压缩更少)。

    • launch.json 会映射到二进制文件

      "program": "${workspaceFolder}/a.out",

    • task.json 会涉及到如何编译

      “参数”:[ “-G”, "${workspaceFolder}/*.cpp", “-o”, "${workspaceFolder}/a.out"

    https://www.youtube.com/watch?v=X2tM21nmzfk&app=desktop

    如果您无法通过 vscode 使其工作,您可能需要使用其他工具,例如 GDB。 GDB 在 Linux/VM 的终端和 WSL 中也能很好地工作。

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 2017-09-23
      • 2022-08-18
      • 2022-12-15
      • 2022-07-22
      • 2015-10-25
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多