How to run C++ in VSCode

How to run C++ in VSCode

ยท

4 min read

Introduction

Hey folks, if you are coding in C++ and using a dedicated IDE for it and want to run all your C++ programs in VSCode editor which we all have come to love very much. Then you have come to the right place.

As you may know, you can just run the code of many languages after installing them, like Python where you have to only install Python and you are good to go, however when you are dealing with C++ you can't do the same thing as it requires a compiler, there are many C++ compilers available.

We will be using MinGW as of now and I will also share some cool tools that will make the process of running the programs much easier, so let's get started.

Step 1 - Downloading MinGW

Firstly we will download the MinGW.

image.png

You can simply click on the green button to start the download of the setup file. Now open the setup file and click on continue, after that it will start downloading some files.

image.png

Step 2 - Choosing the packages

After you click on install you will see a whole lot of packages there firstly look on the left side and select the basic packages option, as we will not be needing any other package as of now.

image.png

Select all the packages available in the basic packages section. And click on Installation, then click on apply changes.

Then it will download all the required files, it will take 5-10 minutes depending on your internet connection and your machine.

Step 3 - Setting environment variables

This is the most crucial part of this whole setup, if we mess this up the programs might not run so pay close attention when doing this step. Open the MinGW folder or wherever you installed it. After that go to the 'include' subfolder and copy the folder path.

image.png

Now, open the control panel and then click on systems.

image.png

Then, click on 'Advanced system setting'

Or you can simply search for it in the start. You can see below what it will look like, click on 'Environment Variables'.

image.png

We are almost there, just a bit more.

Click on 'Path', then click on edit it will open a list of path variables, just click on new and paste the path that we have previously copied.

image.png

And now repeat the same for the path in the system variable section too.

image.png

Now repeat the whole process for 'bin' and 'lib' folder too.

image.png

Just restart your PC as it may help in finalizing the settings.

Congratulations!๐ŸŽ‰ we have successfully installed and configured the compiler to work with our VSCode, now all we have to do is change some settings in the VSCode.

Step 4 - Setting VSCode

Now comes the final step, which is to run our programs in VSCode firstly we will install C/C++ Extension by Microsoft

The C/C++ extension adds language support for C/C++ to Visual Studio Code, including features such as IntelliSense and debugging.

image.png

There is one more extension we need to install before jumping into the juicy stuff i.e., Code Runner by Jun Han

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, Elixir, Visual Basic .NET, Clojure, Haxe, Objective-C, Rust, Racket, Scheme, AutoHotkey, AutoIt, Kotlin, Dart, Free Pascal, Haskell, Nim, D, Lisp, Kit, V, SCSS, Sass, CUDA, Less, Fortran, Ring, Standard ML, and custom command

image.png

It will enable us to run our program with just a click or a key shortcut, isn't that just awesome.

Here comes the part we have all been waiting for, running a C++ program in VSCode.

#include<iostream>
using namespace std;

int main()
{
    cout<<"Hello world!";
}

image.png

Woo0-hoo!๐Ÿฅณ we successfully ran our C++ program in VSCode.

There is just one more thing we have to do is change our program execution window from Output to Terminal, cause as of now we will not be able to take input in any form through the output window so we will change it to Terminal to take input.

Open the setting in VSCode and search for 'code runner' and look for the below setting and enable it and it's done now our program will execute in the Terminal and will be able to take input.

image.png

Program to get sum of two values

#include<iostream>
using namespace std;

int main()
{
    int a,b,c;
    cout<<"Enter two numbers: ";
    cin>>a>>b;
    c=a+b;
    cout<<"Sum: "<<c;
}

Output

image.png

Thank you for reading, I hope it was helpful. ๐Ÿ˜„

Did you find this article valuable?

Support Pranav Goel by becoming a sponsor. Any amount is appreciated!