<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: camelCase</title>
    <description>The latest articles on DEV Community by camelCase (@camelcase).</description>
    <link>https://dev.to/camelcase</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F2958%2F1d689700-f3f7-4061-9b3d-cad76a59d632.png</url>
      <title>DEV Community: camelCase</title>
      <link>https://dev.to/camelcase</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/camelcase"/>
    <language>en</language>
    <item>
      <title>Debug C++ Code on Windows using VS Code</title>
      <dc:creator>Abhinav Tiwari</dc:creator>
      <pubDate>Sat, 22 Aug 2020 12:16:22 +0000</pubDate>
      <link>https://dev.to/camelcase/how-to-run-vs-code-c-debugger-on-windows-4d3</link>
      <guid>https://dev.to/camelcase/how-to-run-vs-code-c-debugger-on-windows-4d3</guid>
      <description>&lt;p&gt;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 &lt;a href="https://codeforces.com/" rel="noopener noreferrer"&gt;codeforces&lt;/a&gt; but I found out a lot of blockers in front of me when I did it the very first time. &lt;/p&gt;

&lt;p&gt;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 &lt;a href="http://www.codeblocks.org/" rel="noopener noreferrer"&gt;Codeblocks&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Install &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install &lt;strong&gt;Windows Subsystems for Linux(WSL)&lt;/strong&gt; using the instructions from the official documentation &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install your Linux distribution of choice. Just open the &lt;a href="https://aka.ms/wslstore" rel="noopener noreferrer"&gt;Microsoft Store&lt;/a&gt; and select your favorite Linux distribution(Ubuntu in my case).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Next, install the GNU compiler tools and the GDB debugger by typing:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install build-essential gdb
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;After installing the compiler tools, verify that the GNU C++ Compiler and the debugger is installed by using these commands:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;g++ --version
gdb --version
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;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.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir helloworld
cd mkdir
code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Once the VS Code is open on WSL, go to View &amp;gt; Extensions(or Ctrl+Shift+X) and install these two extensions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl" rel="noopener noreferrer"&gt;Remote - WSL extension&lt;/a&gt; : The Remote - WSL extension extension lets you use the Windows Subsystem for Linux (WSL) as your full-time development environment right from VS Code.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools" rel="noopener noreferrer"&gt;C/C++&lt;/a&gt;:  C/C++ extension adds language support for C/C++ to Visual Studio Code, including features such as IntelliSense and debugging.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyvqobkx68swanqvthilu.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Once these two extensions are installed, you will see the context in which the VS code is running is changed to &lt;strong&gt;[WSL: Ubuntu]&lt;/strong&gt; in the status bar present at the bottom of Visual Studio.&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fje67apoyq6tb4no4k1wp.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fje67apoyq6tb4no4k1wp.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6exia5jt9zz5zgybccc2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6exia5jt9zz5zgybccc2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// helloWorld.CPP
#include&amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

int lengthOfLIS(vector&amp;lt;int&amp;gt;&amp;amp; a, int n) {
    if(n==0)
        return 0;

    vector&amp;lt;int&amp;gt; dp(n, 0);
    dp[0] = a[0];
    int len = 1;

    for(int i=1; i&amp;lt;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&amp;gt;&amp;gt;n;

    vector&amp;lt;int&amp;gt; v;
    int x;

    for(int i=0; i&amp;lt;n; i++){
        cin&amp;gt;&amp;gt;x;
        v.push_back(x);
    }

    cout&amp;lt;&amp;lt;lengthOfLIS(v, n);
}
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Build the Task:&lt;/strong&gt; After writing the code, go to &lt;code&gt;Terminal &amp;gt; Configure Default Build Task&lt;/code&gt;. In the dropdown, choose &lt;code&gt;g++ build active file&lt;/code&gt;, which will build the file that is currently displayed(helloWorld.cpp in this case) in the editor.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;This will create a tasks.json file in a .vscode folder and open it in the editor. Your new &lt;strong&gt;tasks.json&lt;/strong&gt; file should look similar to the JSON below:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {
    "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
            }
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Next, &lt;code&gt;Run &amp;gt; Add Configuration&lt;/code&gt; and then choose &lt;code&gt;C++(GDB/LLDB)&lt;/code&gt;.&lt;br&gt;
You'll then see a dropdown for various predefined debugging configurations. Choose &lt;code&gt;g++ build and debug active file&lt;/code&gt;. VS Code creates a &lt;strong&gt;launch.json&lt;/strong&gt; file which should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
/****************************************************
 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",
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Now you can start the debugging session. Go back to helloworld.cpp so that it is the active file. Press &lt;strong&gt;F5&lt;/strong&gt; or from the main menu choose &lt;code&gt;Run &amp;gt; Start Debugging&lt;/code&gt;. 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.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fus6xouaaetrscnqlozga.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fus6xouaaetrscnqlozga.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This way you can set breakpoints and go line by line(&lt;a href="https://code.visualstudio.com/docs/cpp/config-mingw#_step-through-the-code" rel="noopener noreferrer"&gt;https://code.visualstudio.com/docs/cpp/config-mingw#_step-through-the-code&lt;/a&gt;) 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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So that is it. Hope it helps in debugging the C++ code on Windows OS and making lives easier. &lt;/p&gt;

</description>
      <category>vscode</category>
      <category>windows</category>
      <category>windowssubsystemsforlinux</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
