Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 567
Arn Vanhoutte Avatar asked Oct 12 '16 12:10

Arn Vanhoutte


1 Answers

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.

like image 73
Chris Avatar answered Sep 23 '22 01:09

Chris