Search code examples
c++jsonlinuxvisual-studio-code

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
}

Solution

  • 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"
    }