How to run C++ Code in Visual Studio Code

Ankan Das
Ankan Das
Apr 1 · 4 min read
Vs Code & C++ Code

I am writing this article because I had faced several issues while running the C++ code in VS Code. As usual, everyone was using the CodeBlocks IDE and DevC++ and many more. But I was already used to Visual Studio Code for all my programming stuff. In this article, I’ll show you how to set up your compiler in VsCode. I will be using a Windows OS throughout this article.

So let’s Start..

Prerequisites:

  1. Visual Studio Code Editor ( Download from here)
  2. Good Internet Connection !!

Download and install a C++ compiler:

After downloading, install MinGW and wait for the “MinGW Installation Manager” to show up.

When the “MinGW Installation Manager” shows up, click on mingw32-gcc-g++ then select “Mark for Installation”.

In the menu at the top left corner, click on “Installation > Apply Changes

Wait and allow to install completely. Ensure you have a stable internet connection during this process.

Edit your PATH environment variable to include the directory where the C++ compiler is located :

  • Click here for a Windows OS guide
  • Click here for Linux
  • Click here for a Mac OS guide

Install Code Runner extension in VS Code :

Code Runner allows you to Run code snippet or code file for multiple languages: C, C++, Java, JavaScript, PHP, Python, Perl, Perl 6, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F# Script, F# (.NET Core), C# Script, C# (.NET Core), VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml Script, R, AppleScript etc.

Search in VsCode marketplace tab:

This is It !!!! We are almost there!!

After installing restart VsCode. Open your C++ file in VsCode.

Here’s a basic hello world program below:

Save this file as first.cpp

Run your code using Code Runner:

2. Press F1 and then select/type Run Code

3. Right-click the Text Editor and then click Run Code in the editor context menu.

The code will run and the output will be shown in the Output Window. Open the output window with `Ctrl+ shortcut.

Bingo !!! We have done it.

DO NOT SKIP THIS STEP :

  • In VsCode, Go to File > Preference > Setting.
  • In the User tab on the left panel, find the extensions section
  • Scroll and find ‘Run Code Configuration
  • Scroll and find a checkbox Run in Terminal (Whether to run code in Integrated Terminal) Check the box.

OR

  • In your setting.json file, add:

Well,you’re done and ready to roll !!! Happy Coding !!

Thanks for reading & if you find this article useful then share this with your programming community to help each other !!

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Ankan Das

Written by

Ankan Das

I’m a Web Developer and Content Creator on Youtube . To know more about me lets connect : https://ankandaslinks.netlify.app/

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store