Introduction

In this blog, we’ll setup our computer so that we can run C++ code. I’ve Windows operating system. Depending upon your OS, method might be different. Following steps are involved in this process: –

  • Downloading Mingw compiler
  • Setting Mingw compiler in system path
  • Download VS code
  • Installing required extensions
  • Setting up launch.json and tasks.json to finally run C++ code.

Requirements

Make sure to download these: –

Mingw

After downloading it, install it. Later, add it to the system path with the following procedure: –

  • Open “system properties”.
  • Click “Environment Variables”.
  • Under “System variables” section, select “PATH” and click on “Edit”.
  • You’ll see “Edit Environment Variables” window bar. Click “New” from the right side and add path “C:\mingw64\bin“.
  • And then click “OK”.

To check whether it’s added to system path correctly or not, type this in cmd: gcc –version

Visual Studio Code

After you download VS code, install it. Create a new folder, within this folder, create a new .cpp file i.e. main.cpp. Also, create a “bin” folder where all our binaries will be present after running the code. This is only meant to keep things clean.

Following are the extensions usually used in VS code to make your work experience better: –

  • C/C++ IntelliSense, debugging, and code browsing. (by microsoft)
  • Code runner

Just go to “Extensions” icon on the left vertical bar of VS code or press Ctrl+Shift+X and search the names of the given extensions and install them.

In the main.cpp file, write a simple c++ program printing “Hello world”.

#include <iostream>
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

If you run this, it won’t work. Because VS code requires some additional information to really collect each and everything to run C++ code. Here comes the need of two files: –

  1. launch.json
  2. tasks.json

Press Ctrl+Shift+P → type “Tasks: Configure Default Build Task” → choose “C/C++: g++ build active file”. It will create or update a file .vscode/tasks.json.

If you want to debug (set breakpoints, step through code):

  1. Go to the Run & Debug panel (left sidebar).
  2. Click “create a launch.json” → choose C++ (GDB/LLDB).
  3. Pick gcc.exe as the compiler (it will auto-detect).
  4. It’ll create a .vscode/launch.json for you.

In case you don’t see gcc.exe at step 3, click C/C++: (gdb) Launch. It helps you to launch your program with GNU debugger, which is compatible with your MinGW-W64 GCC setup.

In launch.json, the "program" field is currently:

"program": "enter program name, for example ${workspaceFolder}/a.exe",

Change it to

"program": "${workspaceFolder}/bin/${fileBasenameNoExtension}.exe",

Update “tasks.json” in this way: –

Change this part:

"${fileDirname}\\${fileBasenameNoExtension}.exe"

To this

"${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe"

Conclusion

Now, you can easily run and debug your C++ code in Visual Studio Code.

Categorized in:

Uncategorized,

Last Update: May 6, 2025