Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup: C++ in VS Code under Linux (tasks.json and launch.json)

How to configure VS Code launch.json for C++ development under Linux?

The official documentation does not provide a launch.json sampleofficial documentation for WSL.

Unfortunately, I can not get a succesful build and debug configuration of tasks.json and launch.json for "pure Linux/no WSL".

I am looking for recent versions of the two files, that are as generic as possible (use variables wherever possible in the json-files).

My current c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
like image 923
tkazik Avatar asked Apr 17 '26 17:04

tkazik


2 Answers

Try this .vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

And .vscode/tasks.json:

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                // "cwd": "/usr/bin"
                "cwd": "${workspaceFolder}"
            }
        }
    ],
    "version": "2.0.0"
}
like image 166
wasmup Avatar answered Apr 19 '26 06:04

wasmup


Setup to run make and GDB specific executable main.out

Here's a version to build a default target with make and then run the default output produced main.out, unlike https://stackoverflow.com/a/56406619/895245 which is for when you want to build and run individual files separately.

First make sure that the C/C++ VS Code extension is installed. Then use:

.vscode/tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "make",
            "args": [],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": "$gcc"
        }
    ]
}

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main.out",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            // Refers to the "label" field in tasks.json.
            "preLaunchTask": "build",
        }
    ]
}

With this:

  • Ctrl + Shift + B: make (default build task due to "isDefault": true)
  • F5: make and gdb main.out. Starts execution and stops at main.

Tested on Ubuntu 24.04, vscode 1.91.1.

The rest of the test files:

main.cpp

#include <iostream>

int main() {
    std::cout << "hello world" << std::endl;
}

Makefile

.PHONY: clean

main.out: main.cpp
    g++ -o '$@' '$<'

clean:
    rm main.out


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!