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

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 placing each task argument in their own string within the args array doesn't work, you can also try placing all of the arguments in a single string within the args array. Example:

["gcc -Wall ${relativeFile} -o ${relativeFile}.exe -pedantic"]

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.


If anyone else comes across this when searching like I did, you can now set the property preLaunchTask in your launch.json to your build task's name property and it will run before your launch.

For Example

"name": "Debug (gdb) Launch", "preLaunchTask": "Build All",

Will run the "name": "Builld All" in your tasks.json before launching your program.

You can read the information on this on the Debugging in Visual Code docs page.


You can create a task for build and as the arguments of it you can pass the commands for running. As an example the task I use to compile and run c++ is shown below.

{

"version": "2.0.0",
"tasks": [
    {
        "label": "g++ build and run",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g",
            "-o",
            "out.exe",
            "\"${file}\"",
            "&&",
            "./out.exe",
            "<",
            "input.in",
            ">",
            "output.out"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]
}

In the above task I compile my source file (the name of the file could be anyone here as ${file} is used) into out.exe and the run out.exe while getting input from input.in and outputting the output to output.out.