C/C++ VS 代码扩展引发构建错误:““C/C++”任务的任务提供程序意外提供了“shell”类型的任务。”

Posted

技术标签:

【中文标题】C/C++ VS 代码扩展引发构建错误:““C/C++”任务的任务提供程序意外提供了“shell”类型的任务。”【英文标题】:C/C++ VS Code extension throwing build error: "The task provider for "C/C++" tasks unexpectedly provided a task of type "shell"." 【发布时间】:2020-08-24 14:54:59 【问题描述】:

当我尝试在 VS Code 中构建 C 任务时,它显示以下消息:

输出只是显示:The task provider for "C/C++" tasks unexpectedly provided a task of type "shell".

我仍然可以使用gcc 'filename.c' -o 'output.exe' 在 cmd 中手动构建我的 C 文件。转到Terminal -> Run Task 而不是使用快捷键 CTRL+SHIFT+B 似乎也可以。

我将 0.28.0-insiders2 C/C++ VS Code 扩展与 MinGW 一起使用。 VS Code 今天刚刚更新到 v. 1.45,我相信这可能是导致此错误的原因,因为我以前没有遇到过。

tasks.json:

  
    "version": "2.0.0", 
    "tasks": [
        
            "label": "Makefile Debug_gcc",
            "type": "shell",
            "command": ["mingw32-make"],
            "args": [
                "--directory=$fileDirname/", 
                "DEBUG=1", 
                "EXECUTABLE=$fileBasenameNoExtensionDebug"
            ]
        ,
        
            "label": "Makefile Release_gcc",
            "type": "shell",
            "command": ["mingw32-make"],
            "args": [
                "--directory=$fileDirname/", 
                "DEBUG=0", 
                "EXECUTABLE=$fileBasenameNoExtensionRelease"
            ]
        ,
        
            "label": "Release",
            "type": "shell",
            "command": "gcc",
            "args": [
                "$file",
                "-o",
                "$fileDirname/$fileBasenameNoExtensionRelease"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "problemMatcher": [
                "$gcc"
            ]
        ,
        
            "label": "Debug",
            "type": "shell",
            "command": "gcc",
            "args": [
                "$file",
                "-g3",
                "-o",
                "$fileDirname/$fileBasenameNoExtensionDebug"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "problemMatcher": [
                "$gcc"
            ]
        ,
        
            "label": "Makefile Debug",
            "type": "shell",
            "command": ["del /S *.o"],
            "dependsOn": [
                "Makefile Debug_gcc"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "problemMatcher": [
                "$gcc"
            ]
        ,
        
            "label": "Makefile Release",
            "type": "shell",
            "command": ["del /S *.o"],
            "dependsOn": [
                "Makefile Release_gcc"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "problemMatcher": [
                "$gcc"
            ]
        ,
        
            "label": "Makefile Debug + Execute",
            "type": "shell",
            "command": "$fileDirname/$fileBasenameNoExtensionDebug",
            "dependsOn": [
                "Makefile Debug"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "problemMatcher": [
                "$gcc"
            ]
        ,
        
            "label": "Makefile Release + Execute",
            "type": "shell",
            "command": "$fileDirname/$fileBasenameNoExtensionRelease",
            "dependsOn": [
                "Makefile Release"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "problemMatcher": [
                "$gcc"
            ]
        ,
        
            "label": "Debug Execute",
            "type": "shell",
            "command": "$fileDirname/$fileBasenameNoExtensionDebug",
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "problemMatcher": [
                "$gcc"
            ]
        ,
        
            "label": "Release Execute",
            "type": "shell",
            "command": "$fileDirname/$fileBasenameNoExtensionRelease",
            "group": 
                "kind": "build", 
                "isDefault": true
            ,
            "problemMatcher": [
                "$gcc"
            ]
        
    ]

【问题讨论】:

根据this issue comment,很快就会修复。 您需要更改“C-Cpp:更新频道”设置,在上面@Allan Chain的链接中查找muhammad-osaid-tekfocal的评论,或直接进入评论@ 987654322@ 之后您也可以更改设置,请参阅 Thomas 的评论(为我工作):github.com/microsoft/vscode-cpptools/issues/… 【参考方案1】:

导航到 .vscode -> 并在其中添加以下两个文件。这是我的文件 vs 代码文件,用于编译、调试和运行 c 代码task.json


    "tasks": [
      
        "type": "shell",
        "label": "gcc.exe build active file",
        "command": "F:\\MinGW\\bin\\gcc.exe",//Mention directory to gcc compiler
        "args": [
          "-g",
          "$file",
          "-o",
          "$fileDirname\\$fileBasenameNoExtension.exe"
        ],
        "options": 
          "cwd": "F:\\MinGW\\bin"//Mention dir to bin foler of Mingw
        
      ,
      
        "type": "shell",
        "label": "gcc build & run active file",
        "command": "F:\\MinGW\\bin\\gcc.exe",//Dir to gcc compiler
        "args": [
          "$file",
          "-o",
          "$fileDirname\\$fileBasenameNoExtension.exe",
          "&&",
          "$fileDirname\\$fileBasenameNoExtension.exe"
        ],
        "options": 
          "cwd": "F:\\MinGW\\bin"//Dir to Mingw's bin folder
        ,
        "group": 
            "kind": "build",
            "isDefault": true
        
      
    ]

launch.json


    "version": "0.2.0",
    "configurations": [
      
        "name": "gcc.exe build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "$fileDirname\\$fileBasenameNoExtension.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "$workspaceFolder",
        "environment": [],
        "externalConsole": false, //set to true to see output in cmd instead
        "MIMode": "gdb",
        "miDebuggerPath": "F:\\MinGW\\bin\\gdb.exe",//Mention the path to gdb inside bin folder of Mingw
        "setupCommands": [
          
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          
        ],
        "preLaunchTask": "gcc.exe build active file"
      ,
      
        "name": "gcc build & run active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "$fileDirname\\$fileBasenameNoExtension.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "$workspaceFolder",
        "environment": [],
        "externalConsole": false, //set to true to see output in cmd instead
        "MIMode": "gdb",
        "miDebuggerPath": "F:\\MinGW\\bin\\gdb.exe",//Mention the path to gdb inside bin folder of Mingw
        "setupCommands": [
          
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          
        ],
        "preLaunchTask": "gcc build & run active file"
      
    ]

【讨论】:

以上是关于C/C++ VS 代码扩展引发构建错误:““C/C++”任务的任务提供程序意外提供了“shell”类型的任务。”的主要内容,如果未能解决你的问题,请参考以下文章

Cython 是用于构建 C 代码还是用于构建 Python 扩展?

VS Code如何编写C/C++程序

VSCode语法错误突出显示不适用于C ++代码

使用 C++ - C++/CLI - C# 项目(Borland 和 VS 特定组件)构建问题

C/C++Linux下system()函数引发的错误

Shell 扩展:C/C++ 运行时 DLL 的静态链接与动态链接