Visual Studio Code调试器错误:"找不到任务'gcc build active file'"

14

我正在尝试在Ubuntu Linux上使用Visual Studio Code配置C/C++工作区,但我不知道如何使调试器正常工作。我从互联网上复制了一个'tasks.json'文件,以便按F5编译我的代码,但我认为它会导致调试器出现问题,因为每次我尝试进入调试模式时,都会弹出错误“无法找到任务'gcc build active file'”。以下是这两个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"
    }
]

感谢您提前的帮助,我真的非常非常地无助。

2个回答

16
在你的 `tasks.json` 文件中,没有标记为“gcc build active file”的任务,而这是在 `launch.json` 文件中作为 `preLaunchTask` 所需的。
因此,你可以更改任务的 `label` 或更改 `preLaunchTask` 的内容以使它们匹配。
只需将 `preLaunchTask` 的内容更改为“g++ build active file”,它就可以正常工作了。

为了清晰起见,我认为Albert指的是tasks.json文件。 - SunDontShine
它们在我的情况下都匹配,但我仍然遇到了这个错误,我只需要重新启动VS Code,更多信息请参见:https://dev59.com/Cqrka4cB1Zd3GeqPd3Vh#55107773 - Jonathan

3
您需要指定文件的路径和名称。当然,只有在二进制文件使用g标志编译时才能进行调试(输出会更加沉重且不太压缩)。
  • launch.json将映射到二进制文件

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

  • task.json将涉及如何编译

    "args": [ "-g", "${workspaceFolder}/*.cpp", "-o", "${workspaceFolder}/a.out"

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

如果您无法通过VSCode使其工作,则可以使用另一个工具,如GDB。 GDB在Linux / VM和可能的WSL终端中也运行良好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接