Configuring VS Code for C++ programming (Step-By-Step guide)

Jamwal Ajay
6 min readDec 26, 2020

--

Before proceeding further, I would like to tell you that I have never written an article in my life. But the articles I found on the internet for Configuring VS Code for C++, were not well-explained. This motivated me to write this article. I am writing this to help someone who is in the same situation that I was in. Hope this helps you.

Whenever you are programming something, you always require these two basic tools:

a)Text editor: To write or Ctrl-V + Ctrl-C your source code file.

b)Compiler: To convert your source code to a machine-understandable code.

As this is a guide for configuring VS Code for C++, we will be using:

a)VS Code as our text editor.

b)mingw-w64 as our compiler.

Installing both Visual Studio Code and mingw-w64

  1. Install Visual Studio Code.
  2. Now open marketplace in Visual studio (Ctrl+Shift+X) and search & install C/C++ extension for VS Code and Code runner for VS Code extensions.
  3. Install Mingw-w64 via the SourceForge website. Click Mingw-w64 to download the Windows Mingw-w64 installer. Run the installer. While selecting default installation folder make sure to copy the path where Mingw-w64 will be installed. You will require this when you add it to environment variables.

Add the path to your Mingw-w64 bin folder to the Windows PATH environment variable

  1. Open the path of your Mingw-w64 in file explorer and navigate to its bin folder. Copy the whole path this time with the bin folder in it. (The path might look something like this “C:\Mingw\bin” depending on your installation.)
  2. In the Windows search bar, type ‘settings’ to open your Windows Settings.
  3. Search for Edit environment variables for your account.
  4. Choose the Path variable and then select Edit.
  5. Select New and add the Mingw-w64 destination folder path to the system path(The one that you had copied).
  6. Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.

Now to check if the path has been added or not

  1. Open a command prompt (Windows+R, then type cmd and hit ok).
  2. In the command prompt type g++ — version. It will show the version of g++ compiler inside Mingw-w64. If it does not shows the required result or outputs that g++ is not a recognized command, check your installation (Windows Control Panel > Programs) and make sure your PATH entry matches the Mingw-w64 binary location where the compiler is located.

Now at this point in time, both our text editor and compiler are installed and ready to be used.

Create a new folder named HELLOWORLD on your Desktop and navigate into it through the command prompt. Now while you are inside this folder in your command prompt, type the command “code .”

The “code .” command opens the VS Code in your current working folder and it becomes your workspace in VS Code. As we will reach closer to ending this article, you will notice that a new folder name .vscode will get created in your HELLOWORLD workspace folder with three auto-generated files:

  • tasks.json (build instructions)
  • launch.json (debugger settings)
  • c_cpp_properties.json (compiler path and IntelliSense settings)

Let’s write a HelloWorld

In the file explorer bar, click on the new file button and name the file helloworld.cpp. Open the file and copy the below code into that file.

#include <bits/stdc++.h>

using namespace std;

int main(){cout << “HELLO WORLD”;}

Now press Ctrl+S to save the file.

You will notice that there are two errors in your helloworld file.

This is because VS Code is not able to detect any compiler path and IntelliSense configuration for the current workspace. As discussed above we need to add a new file named c_cpp_properties.json in a folder named .vscode.

You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette (Ctrl+Shift+P).

This opens the C/C++ Configurations page. When you make changes here, VS Code writes them to c_cpp_properties.json file in the .vscode folder.

Here we can change the configuration name to GCC, compiler path to the g++ compiler, and IntelliSense mode to match the g++ compiler.

Visual Studio Code places these settings in .vscode\c_cpp_properties.json. If you open that file directly, it should look something like this:

{
"configurations": [
{
"name": "GCC",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/MinGW/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}

Now after saving this file you will notice that the errors get resolved. Also, you can check the IntelliSense by hovering over any method or property in the code.

Now after all this configuration for compiler and IntelliSense settings we are ready to build and execute this code.

The simplest way is by opening the terminal (Ctrl+Shift+`) and giving the command: g++ helloworld.cpp

After this command gets executed, you will note that a new executable file named a.exe gets created in your workspace. You can now execute your code by typing ./a.exe in the terminal.

And there you get the HELLO WORLD you were expecting to see. But we are not done yet. This task of invoking the compiler each time through the terminal gets very cumbersome. Thus it also needs to be automated through configuration.

Configuring build instructions

Now we will create a tasks.json file to tell VS Code how to build (compile) the program.

From the main menu, go to Terminal>Configure Default Build Task. A tasks dropdown will appear in which we have to choose g++.exe build active file, which will build the file that is currently displayed (active) in the editor.

This will create a tasks.json file in the.vscode folder and open it in the editor.

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

Now in tasks.json file, the command settings specify the command that will be used; in this case that is our compiler executable. The args array specifies the arguments that will be passed to the command.

This task tells the compiler to compile the active file(${file}), and create its executable in the current directory(${fileDirname}) with the same name.

Thus in our case, the new executable will be helloworld.exe instead of the default a.exe.

Running the build

  1. Open helloworld.cpp file on your workspace.
  2. Press Ctrl+Shift+B to run the build task defined in tasks.json.
  3. After running successfully the task would display a message as shown below.

4. Press any key and the terminal will open. Now type ./helloworld.exe to execute the new executable. You will see the result as shown below.

And here we have the famous HELLO WORLD again.

Hope this article helps you to understand how to configure VS Code for C++. This was my first ever write up on a blogging platform. Comment down if this was helpful for you, or you are facing some issue. Also, any constructive feedback would be very much appreciated.

--

--

Jamwal Ajay
Jamwal Ajay

Written by Jamwal Ajay

Skater, coder, investor, and an avid learner. Sometimes I write.

No responses yet