VS Code Configuration for Debugging C++ programs

This is the configuration I use for debugging C++ programs in vscode:

Build Tasks

{
 "version": "2.0.0",
 "tasks": [
  {
   "label": "build",
   "type": "shell",
   "command": "/usr/bin/clang++",
   "args": [
    "-std=c++20",
    "-g",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}",
    "${file}"
   ],
   "group": {
    "kind": "build",
    "isDefault": true
   },
   "detail": "Generated task to build the project"
  }
 ]
}

Note

  • You might need to set the command property to a different directory. Use which clang++ or which g++ in your terminal.

Launch

{
 "version": "0.2.0",
 "configurations": [
  {
   "type": "lldb",
   "request": "launch",
   "name": "Debug",
   "program": "${workspaceFolder}/${relativeFileDirname}/${fileBasenameNoExtension}",
   "cwd": "${fileDirname}",
  }
 ]
}

Notes

  • I have the type set to lldb. If you are on a mac and use the defaultclang++, you will need to install this extension.
  • If you are on Windows (ew) or Ubuntu (eww) or any other OS, you might want to set the type to gdb.
  • If you want to take input using a file, add this after the cwd line:

    "initCommands": [
    "settings set target.input-path ${workspaceFolder}/${fileBasenameNoExtension}.input",
    ]
    
    • I usually have the input files name the same as the source file name with a .input extension added (usually during codeforces contests).