We are going to see how to configure microsoft visual studio code for running c and c++ program
Installation of MinGW
- Download MinGW from link given below.
Download Link : https://sourceforge.net/projects/mingw/
- Run mingw-get-setup.
- A window will be appeared like this , Click on
Install
- Continue with default Installation Directory or change directory by clicking on
Change
and click onContinue
- An Installation wizard as shown above will be visible after clicking on
Continue
, wait for the download process to be completed
- Click on
Continue
- A Package installtion window will appear as shown in the image above . Click on each package >> Select
Mark for Installation
- Select all package as shown above
- Click on
Installation
>> SelectApply Changes
- A Prompt window will appear . Click on
Apply
- A window will appear as shown above . Wait until the download get completed
- After the installation of all packages . Click on
Close
Adding Path
- Open File Explorer
- Right-Click on
This PC
>> Click onProperties
- Click on
Advanced system settings
- Click on
Environment Variables...
- Click on
New...
in User variables
- Give Variable name:
Path
and Variable value:C:\MinGW\bin
. Click onOK
, Change Variable value, if you have given different directory location at time of installation of MinGW
- Now we can see in the above image, Path is added in User variables
- Now to verify all the steps are followed correctly , Open Command Prompt and Type
g++ --version
- Then type
gdb --version
- If you get similar output after running both command , you have successfully installed MinGW and set path
VS Code Installation and Setting up
- Download Visual Studio Code from Download Link given below.
Download Link : https://code.visualstudio.com/download
- Run VSCodeUserSetup .
- Check
I accept the agreement
,ClickNext>
- Continue with default directory location or Change directory location by Clicking on
Browse...
, Click onNext>
- Click on
Next>
- Check all required Additional Tasks , Click on
Next>
- Click on
Install
- Check
Launch Visual Studio Code
, Click onFinish
- You will see Visual Studio Code as shown above when launched for first time
- Click on Extensions as shown in the above image or use
Ctrl
+Shift
+X
- Search for c/c++ in search box as shown above image , Select C/C++ , Click on
Install
- Search for code runner in search box , Select Code Runner , Click on
Install
- Click on Explorer as shown in the above image or use
Ctrl
+Shift
+E
- Click on use
Open Folder
- Create a folder or Select a folder . Click on
Select Folder
- Click on New Untitled File as show in the above image
- Create a file with *.cpp as extenstion as shown in image above
- Click on
Run
>>Add Configuration...
- Select
C++(GDB/LLDB)
- Select
g++.exe
- After a while you can see a similar screen as shown above with configuration file launch.json
- Select *.cpp file (test.cpp in my case) which is blank file
#include<iostream>using namespace std;main(){cout <<"hello";}
- Copy the above code
- Paste it in *.cpp file and save it , use
Ctrl
+S
- Click on
Termianl
>>Run Build Task...
or pressCtrl
+Shift
+B
- If Build is successful you will get a message : (Build finished successfully.) as shown above in the image
- Restart Visual Studio Code or Close and Reopen Visual Studio Code
- Click on Run Code button as shown in the image above
- the ouput hello is printed in TERMINAL as shown in the image above
After ward you can change program or create new file with extenstion (*.cpp) for c++ program or (*.c) for c program , save it , then run code using Run Code button