Skip to content

Instantly share code, notes, and snippets.

@rain1024
Last active May 23, 2022 09:31
Show Gist options
  • Save rain1024/2adda9562b9b7ceb61b07f020236deac to your computer and use it in GitHub Desktop.
Save rain1024/2adda9562b9b7ceb61b07f020236deac to your computer and use it in GitHub Desktop.
Debug C++ on Ubuntu 20.04 with Visual Studio Code

Debug C++ on Ubuntu with Visual Studio Code

Requirements

Instructions

Step 1: Edit launch.json file

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "lldb launch",
      "type": "lldb",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "preLaunchTask": "C/C++: g++ build active file"
    }
  ]
}

Step 2: Edit tasks.json file

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-std=c++17",                
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}

Step 3: Create and edit main.cpp file

#include <iostream>
#include <vector>
using namespace std;

int main(){
    string s = "Hello World";
    vector<int> v = {0, 1, 2, 3};
    for(auto item: v){
        cout << "Hello World " << item << endl;
    }
    return 0;
}

Step 4: Run & Debug

Click Run & Debug (Ctrl+Shift+D), then click Start Debugging (F5)

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment