Arn Vanhoutte Arn Vanhoutte - 1 month ago 30
C++ Question

How to build and run C++ code in Visual Studio Code?

I have a tasks.json script that currently compiles the code

{
"version": "0.1.0",
"command": "gcc",
"isShellCommand": true,
"args": ["-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"],
"echoCommand": true,
"showOutput": "always",
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}


This works fine, but when I want to run the file I have to run the exe from command line. Is it possible to do this in the task as well? So if it finishes building succesfully it then runs a different task?

Answer

You can configure multiple tasks in Visual Studio Code, one of which will allow you to build your executable, and the other will run your executable.

Optionally, you could also look into Visual Studio Code's "Run Mode" (see here). If you use "Run Mode", you should be able to configure Visual Studio Code to build your executable, and then launch it.

I'm not extremely familiar with "Run Mode", thus I will detail how to define multiple tasks to achieve a similar result.


Disclaimer: Visual Studio Code does not support tasks that use different shell commands (see here).

That's right. At its current state, Visual Studio Code doesn't have "native" support for defining tasks that use different shell commands.

Disclaimer: Visual Studio Code's task-output pane will not allow you to pass input to your program interactively.

If your program relies on user-input (for example, from stdin), you're probably better off not using Visual Studio Code to run your executable.


Basically, what we'll need to do, is define two tasks, one of which will be a build task, the other will be our launch task.

Seeing as Visual Studio Code doesn't have great support for defining multiple tasks that each use different shell commands, we'll need to change our tasks.json's "command" property to cmd (or sh, if on Linux/macOS). We'll also need to set the "args" property to [/C] ([-c] if on Linux/macOS).

The reason behind us doing this, is because we want each of the tasks we're about to define, to be passed as arguments to a new shell instance.

The next step, is to define our build and launch tasks. When we do so, we'll need to make sure we place the command we want to run, as a task argument. For example:

{
    "taskName": "build",
    "args": ["gcc", "-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"]
}

Finally, what we'll do, is add the "isBuildCommand" property to our build task (and make sure it's true), as well as add the "isTestCommand" property to our launch task (and, again, make sure it's true).

After all of that, our tasks.json file could look something like this:

{
    "version": "0.1.0",
    "command": "cmd",
    "args": ["/C"],
    "isShellCommand": true,
    "showOutput": "always",
    "suppressTaskName": true,
    "tasks": [
        {
            "taskName": "build",
            "args": ["gcc", "-Wall", "${relativeFile}", "-o", "${relativeFile}.exe", "-pedantic"],
            "isBuildCommand": true
        },
        {
            "taskName": "run",
            "args": ["${relativeFile}.exe"],
            "isTestCommand": true
        }
    ]
}

Note: If you would like to be able to invoke your task(s) via keyboard shortcuts, you have the "workbench.action.tasks.build" and "workbench.action.tasks.test" editor commands at your disposal.

If you need an example of binding keys to those commands, here's an example of how I have them mapped in my keybindings.json file:

[
    {
        "key": "f6",
        "command": "workbench.action.tasks.build"
    },
    {
        "key": "f7",
        "command": "workbench.action.tasks.test"
    }
}

Edit: You probably only need to define a keyboard shortcut for the test task, as the build task probably already has one defined. Check here before you take the time to define a different keyboard shortcut.