How To Run C++ in Visual Studio Code on Windows 11

As developers, we are always looking for ways to improve our workflow and make our lives easier. One issue that many developers face is getting their development environment set up and working properly. In this post, we will show you how to run C++ in Visual Studio Code on Windows 11.

Video Tutorial : How to Run C++ in Visual Studio Code on Windows 11

Step 1: Download the C++ Compiler for Windows 11

1. We need to download the C++ compiler for our windows 11 machine. We’re going to download the “MinGW” compiler. Click on the below link to open the download page for MinGW Compiler.


Download MinGW Compiler Link: https://techdecodetutorials.com/download/

2. We will be downloading a self-extractable archive for MinGW to make the installation process easy. So we will click on the “Direct Download Link” and the download will start.

Step 2: Extract Zip File

1. Once it has been downloaded, you need to open it from the folder and then double-click on it to start the installation process.

2. Choose the extraction location of the compiler by typing “C:\” in the text field and then click the extract button to start the extraction process. The extraction of this archive will take some time.

Step 3: Set Up Environment Variables

1. First, copy the location of the bin folder from the newly extracted MinGW directory. In our case, it looks like this: “C:\MinGw\bin“. Next, we will set up the environment variables for accessing g++ in vscode.

2. Now click on the Windows button and then type Environment Variables in the search field and press Enter

3. Click on the Environment Variable button.

4. Now, select the Path variable and click on the Edit button.

5. Click the "New" button, then paste the following location in the blank field and click "OK."

Location: C:\MinGw\bin

Step 4: Check Verify G++ Version

1. To check if the MinGW compiler is working properly, open windows command prompt by pressing the Windows button and type cmd, then press enter.

2. In the command prompt type,  g++ --version and press enter key to check the installed version of MinGW g++.

Step 5: Install C++ Extensions in Visual Studio Code on Windows 11

Also Read: How To Install Visual Studio Code in Windows 11

1. Open visual studio code, Press Windows keys and then type Visual Studio Code and press enter.

2. Click on the extension button and in the search bar type C/C++, then select the first option from Microsoft and click on the small blue install button.

3. Now again in the extensions search bar type Code Runner, select the first option from Jun Han and click on the small install button.

with this extension, you can easily run the code by pressing Ctrl+Alt+N and stop the code by pressing Ctrl+Alt+M

Step 6: Run C++ in Visual Studio Code on Windows 11

1. Now create a new file and save it as  subscribe.cpp


Make sure to add “.cpp” extension at the end of the filename as without it visual studio code won’t be able to detect the code as a c++ program

2. Paste the following code snippet and save your code by pressing Ctrl+S

#include<iostream>
using namespace std;
int main()
{
    cout<<"Subscribe Now!";
    
    return 0;
}

3. To run the C++ program you can press Ctrl+Alt+N or you can simply press the play button on the top right and you’ll see an output like the below image.

Output

Step 7 [Bonus]: Fix Cannot Edit in reading Only Editor in Visual Studio Code

1. Create a new file in visual studio code and save it as an inputProgram.cpp

2. Paste the following code snippet and save your code by pressing Ctrl+S and run the code by pressing Ctrl+Alt+N or run play button.

#include<iostream>
using namespace std;
int main(){

    string fname,lname;
    cin>>fname;

    cout<<"Enter your first name: ";
    cin>>fname;

    cout<<"Enter your last name: ";
    cin>>lname;

    cout<<"Your full name is "<<fname<<" "<<lname;

    return 0;
}

3. It will show the output but you will not be able to enter your first name

4. To fix this, Open settings by pressing Ctrl+ and in the search bar type Run In Terminal and hit enter. Now Scroll down a little bit and tick the Code Runner: Run in Terminal checkbox.

5. Now go back to the program we’ve created in step 2 and press Ctrl+Alt+N this time you will be able to enter your first name and last name.

Now start your journey in c++ programming 

 Click Here To Enroll in Complete C++ Programming Course: Go From Beginner to Beyond with an additional discount.

 Click Here To Enroll in Complete C Programming Course: Go From Beginner to Master with an additional discount.

Leave a Reply

Your email address will not be published. Required fields are marked *