Open In App

VS Code | Build, Run and Debug in C++

In this article, we will discuss the VS Code setup required for break-point debugging. Firstly create a file launch.json that configures the VS Code to launch the GDB debugger at the beginning of the debugging process. Then create a file tasks.json that tells VS Code how to build (compile) the program. Finally, make some changes to the console settings and implements the build and debugging.

Program:
Let below be the code for the demonstrate purposes:






// C++ program to find the value of
// the pow(a, b) iteratively
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    int a, b, pow = 1;
 
    // Input two numbers
    cin >> a >> b;
 
    // Iterate till b from 1
    for (int i = 1; i <= b; i++) {
        pow = pow * a;
    }
 
    // Print the value
    cout << pow;
}

Launch.json

This file pertains to information like the name of the debugger, path to the debugger, current CPP file’s directory, and console regarding data. Below is the code in the file launch.json:

{
    // 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++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname} 
              \\${fileBasenameNoExtension}.exe",
              // There I have got error Property keys must be doublequotedjsonc
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

Important terms in launch.json: You can either use entirely code given above or otherwise you may use the code generated in your machine automatically. In both cases, you have to take some precautions. Firstly, make sure that the attribute “externalConsole” is marked as true (we will understand its significance later in this topic). Then check whether the property “miDebuggerPath” points to the gdb debugger correctly. Let us discuss these attributes in detail:



  1. name and type: These are quite self-explanatory, these refer to the name and type of launch.json.
  2. request: states the type of JSON file.
  3. program: stores the detailed address of the program for which launch.json file.
  4. cwd: denotes current working directory. Note that all the addresses in launch.json file are in general form, they are not specific to any file.
  5. externalConsole: as there is no way to handle input/output in VS Code’s integrated terminal, use an external console for this. So set it to true.
  6. miDebuggerPath: points to the location of debugger, this will vary from user to user.
  7. preLaunchTask: this contains the name of tasks.json file.

Note: If you are not able to find any of the executable listed above (such as gdb.exe or g++.exe), you may have to install them manually. You can use MinGW Install Manager to install the required components.

Steps:

Tasks.json:

This file contains information like the commands used for compilation, the compiler’s address, the same label as in launch.json, and some other information. Below is the code in the file task.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-std=c++11",
                "-O2",
                "-Wall",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}
                 \\ ${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "isDefault": true,
                "kind": "build"
            }
        }
    ]
}

Important terms in task.json: The above mentioned tasks.json file can be used entirely without caution. However, the only thing to take care of is that the label of tasks.json should match with preLaunchTask of Launch.json. Let’s discuss some terms in more detail:

g++ -std=c++11 -O2 -Wall ${file} -o ${fileDirname}\\${fileBasenameNoExtension}.exe

Steps:

Console Adjustments

Steps:




// C++ program to find the value of
// the pow(a, b) iteratively
#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
 
// Driver Code
int main()
{
    int a, b, pow = 1;
 
    // Input two numbers
    cin >> a >> b;
 
    // Iterate till b from 1
    for (int i = 1; i <= b; i++) {
        pow = pow * a;
    }
 
    // Print the value
    cout << pow;
 
    _getch();
}

Break-point Debugging:

Steps:


Article Tags :