Search code examples
cvisual-studio-codebuildvscode-tasks

[Visual Studio Code]: Run task on save


I want do develop C programs with vscode.

My tasks.json, which describes the build stage if I understood that correctly, looks like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "runOptions": {
                "runOn": "default"
            }
        }
    ]
}

Since all the compiler errors are generated by the build, I would like to run it every time I save the file. This allows me to get immediate compiler feedback.

How can I set this up this? Note that most Plugins like Run on Save don't work, since I can't specify the task, just the command, and then I won't get the compiler errors.


Solution

  • You can use the Trigger Task on Save extension.

    To set it up, add the following to your .vscode/settings.json:

    "triggerTaskOnSave.tasks": {
      "C/C++: gcc build active file": [
        "*", // <-- Glob pattern to specify which files should trigger the task
      ],
    }