Dmitri Nesteruk
Dmitri Nesteruk

Reputation: 23798

How to configure VS Code to compile/debug C++?

I've installed the C/C++ extension for VS Code but am not quite sure what I need to have in my tasks.json in order to compile a project. Is there an example I can look at somewhere?

Also, the extension refers to Clang tools, I kind of assumed that Clang doesn't work on Windows.

Upvotes: 0

Views: 2727

Answers (1)

Pieter de Vries
Pieter de Vries

Reputation: 825

Here is a webpage where they explain more about the task.json file.

https://code.visualstudio.com/docs/editor/tasks

The build tasks are project specific. To create a new project, open a directory in VSCode.

Following the instructions here, press Ctrl+Shift+P, type Configure Tasks, select it and press Enter.

The tasks.json file will be opened. Paste the following build script into the file, and save it:

{
    "version": "0.1.0",
    "command": "make",
    "isShellCommand": true,
    "tasks": [
        {
            "taskName": "Makefile",
            // Make this the default build command.
            "isBuildCommand": true,
            // Show the output window only if unrecognized errors occur.
            "showOutput": "always",
            // No args
            "args": ["all"],
            // Use the standard less compilation problem matcher.
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

Now go to File->Preferences->Keyboard Shortcuts and add the following key binding for the build task:

// Place your key bindings in this file to overwrite the defaults
[
    { "key": "f8",          "command": "workbench.action.tasks.build" }
]

Now when you press F8 the Makefile will be executed and errors will be underlined in the editor.

Upvotes: 2

Related Questions