Debugging C and C++ with VSCode

Let us setup our favourite editor — Visual studio code to have debugging support for c and c++ projects or files. If you don’t have vscode editor, I suggest downloading it from here and have a play with it. It’s a good community driven and Microsoft effort to have a nice ecosystem around JavaScript and TypeScript apps.

Visual studio Code — Credits: Microsoft

Now to have debugging enabled for C/C++ language, please download the C/C++ extension from Microsoft from the extensions tab as shown above. Just click ‘Install’ and it will setup automatically.

I’m on osx and love `clang` support with `lldb` which makes it easy to debug string, collections nicely. You can type a basic c++ program and use lldb to debug it like below.

#include <iostream>
using namespace std;
int main() {
string message = "Hello World";
cout << message << endl;
return 0;
clang++ and lldb debugging

Ok but we want nicety of debugging with visual editor and do the same step into and analysing tasks visually. So, go ahead and enter the above program by making a new folder and copy-paste it in a file namely `main.cpp`.


Let’s go to debugging tab now and click the gear icon with red dot and select C++ (gdb/lldb) .

Environment for debugging

It will create launch.json file and allow to customise the options for the project. We will change “program” and “externalConsole” fields under “configurations”. Set these two to below values. Do it for both “C++ Launch” and “C++ Attach”. You can select the “name” when you will try to debug the program. “C++ Attach” is useful in cases when the program is running elsewhere from command line or using other IDE. We will be sticking with “C++ Launch” in this article.

"program": "${workspaceRoot}/a.out",
"externalConsole": false

Now press `Cmd + shift + B` on osx or `Ctrl + shift + B` on windows/linux to “Configure Task Runner”.

Task Runner Config

Select “Others” after clicking to “Configure Task Runner”. It will create `tasks.json` file. Replace the file with the contents below.

// See
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "clang++",
"isShellCommand": true,
"args": ["main.cpp", "-std=c++11", "-g"],
"showOutput": "always"

Now you can press `Cmd + shift + B` on osx or `Ctrl + shift + B` on windows/linux to build the program `a.out` with debugging symbols. Go ahead set a breakpoint on line 7 and click on the `run` button in debug tab to start the debugger. You will see something like below.

Final c/c++ debugging with vscode

Not the best support but it works, you can do same operations like step in, out, over, continue, hit breakpoints, inspect etc. There is still on-going improvements in this area as the lldb output of print has better inspection than above. I also tried creating `stack` and analysing it with lldb and vscode, and lldb still have pretty print in there. Outputs shown below.

stack debugging comparison vscode
stack debugging comparison lldb

I hope the above will improve in future but i’m still enjoying the support for debugging, the editor is really fast and a nice syntax highlight support, backed by Microsoft and community effort. Cool!