This article is about using VS Code for Debugging your C/C++ code on Windows(8/9/10). I thought it was a simple task when I was trying to debug my code for one of the problems for an online coding competition on codeforces but I found out a lot of blockers in front of me when I did it the very first time.
The first one of those was setting up a debugger in Windows(Windows 10 in my case) was quite problematic when used for languages like C++. Although I could have used Codeblocks which supports debugging features on Windows platforms in this case, but I am not used to light-theme applications and I don't quite like the debugging interface it offers. Like a lot of other developers, I am a fan of dark-themes too, and using something like VS Code will give me more options to explore in dark themes. For that reason, I decided to get the debugger for C++ running on my Windows system.
After a lot of googling and reading a lot of articles on the same, I was finally able to debug my C++ code using VS Code on my Windows system but it took a lot of my time to work out the complete solution. So I decided to prepare a compiled step-by-step guide for the complete process so that everything can be found at a single place. Here are the steps:
Install Visual Studio Code.
Install Windows Subsystems for Linux(WSL) using the instructions from the official documentation here.
Install your Linux distribution of choice. Just open the Microsoft Store and select your favorite Linux distribution(Ubuntu in my case).
After installation is completed, search for the Linux distribution which you installed in windows search(lets say Ubuntu). Opening it will start the WSL command prompt.
-
Inside the WSL command prompt, first run apt-get update to update the Ubuntu package lists just to avoid any issues because of out-of-date packages:
sudo apt-get update
-
Next, install the GNU compiler tools and the GDB debugger by typing:
sudo apt-get install build-essential gdb
-
After installing the compiler tools, verify that the GNU C++ Compiler and the debugger is installed by using these commands:
g++ --version gdb --version
If both these commands do not return the version currently present on WSL and give some error, try updating/installing the GNU compiler tools once again.
-
Now your Linux environment is fully set up to get the things running. Create a folder for your visual studio project(say helloWorld) and open it in Visual Studio Code in the project folder using following commands.
mkdir helloworld cd mkdir code .
Opening VS Code in WSL command prompt will install a small server(VS Code Server) on the Linux side that the Windows system VS Code will talk to.
-
Once the VS Code is open on WSL, go to View > Extensions(or Ctrl+Shift+X) and install these two extensions:
- Remote - WSL extension : The Remote - WSL extension extension lets you use the Windows Subsystem for Linux (WSL) as your full-time development environment right from VS Code.
- C/C++: C/C++ extension adds language support for C/C++ to Visual Studio Code, including features such as IntelliSense and debugging.
Once these two extensions are installed, you will see the context in which the VS code is running is changed to [WSL: Ubuntu] in the status bar present at the bottom of Visual Studio.
-
Now you are ready to write your code. Add a source code file(say helloWorld.cpp) from VS code and write your code in that file:
// helloWorld.CPP #include<bits/stdc++.h> using namespace std; int lengthOfLIS(vector<int>& a, int n) { if(n==0) return 0; vector<int> dp(n, 0); dp[0] = a[0]; int len = 1; for(int i=1; i<n; i++){ auto it = lower_bound(dp.begin(), dp.begin()+len, a[i]); if(it == dp.begin()+len) dp[len++] = a[i]; else *it = a[i]; } return len; } int main(){ int n; cin>>n; vector<int> v; int x; for(int i=0; i<n; i++){ cin>>x; v.push_back(x); } cout<<lengthOfLIS(v, n); }
Build the Task: After writing the code, go to
Terminal > Configure Default Build Task
. In the dropdown, chooseg++ build active file
, which will build the file that is currently displayed(helloWorld.cpp in this case) in the editor.-
This will create a tasks.json file in a .vscode folder and open it in the editor. Your new tasks.json file should look similar to the JSON below:
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "g++ build active file", "command": "/usr/bin/g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "/usr/bin" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true } } ] }
-
Next,
Run > Add Configuration
and then chooseC++(GDB/LLDB)
.
You'll then see a dropdown for various predefined debugging configurations. Chooseg++ build and debug active file
. VS Code creates a launch.json file which should look like this:
{ /**************************************************** Use IntelliSense to learn about possible attributes. Hover to view descriptions of existing attributes. For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 ****************************************************/ "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", } ] }
Now you can start the debugging session. Go back to helloworld.cpp so that it is the active file. Press F5 or from the main menu choose
Run > Start Debugging
. Do not forget to set the breakpoints(just click on the left of line number) where you want the debugger to stop as shown in the sample image below.
- This way you can set breakpoints and go line by line(https://code.visualstudio.com/docs/cpp/config-mingw#_step-through-the-code) through your code using the debugger options to find where it might be going wrong. Here is some help on how to use a debugger.
So that is it. Hope it helps in debugging the C++ code on Windows OS and making lives easier.
Top comments (1)
How can I visualize the vector that you have declared?
What if I want to see the values in it?