How to Run C and C++ Programs on VS Code - MrAboutTech

Breaking

Wednesday, October 28, 2020

How to Run C and C++ Programs on VS Code

 

GCC + VS Code
Logo Credits: Papirus Development Team


Visual Studio Code or VS Code is an open-source code editor by Microsoft. It is a well know editor among developers. This blog is focused on helping you set up your VS Code to run any C or C++ program on it.


Things you will need

1. VS Code

2. MinGW Installation Manager Setup


So, the first thing that you will need is the VS Code itself. If you have already installed then you can move on to further steps but if you haven’t then you can download it by going to the official website.


After that, you will have to download and install MinGW for installing the GCC and G++ compilers which you will need for compiling your C and C++ programs.


Note: You will need an Internet Connection throughout the process so make necessary arrangements.


MinGW Installation

After downloading the MinGW setup, run it and you will see a screen like below

mingw setup - install


Click on install.


Next, you will have to set the installation drive. The default will be C Drive and I will suggest you keep the default. There will be other user options as well so leave them as they are and click on continue.


mingw setup  - specify installation preference


mingw setup - Downlaod and setup MinGW Installation Manager


After the installation is completed, click on continue.


mingw setup - installation completed


Once you click on continue, MinGW Installation Manager will open.


MinGW Installation Manager


Now here make sure that you are in the Basic Setup section.


MinGW Installation Manager - Basic Setup section


In the Basic Setup, the one package that you need is mingw32-gcc-g++


mingw32-gcc-g++


To install it we will have to make it for installation.


Just click on the package name and you will see the option for Mark for Installation.


Select mingw32-gcc-g++


Once it's done then it will be marked.


Marked mingw32-gcc-g++


Now, go ahead and click on the Installation option in the menu of MinGW Installation Manager and then click on Apply Changes.


As you do that one pop-up will open and there you just have to click on apply to install the package that we marked in the previous step.


MinGW Installation Manager - Apply Package Changes


Now the packages will be downloaded.


mingw32-gcc-g++ package installation


Once its done you will see a successful message.


mingw32-gcc-g++ package installation completed


You have successfully installed GCC and G++ compilers on your system but we are not done yet. It won't work in VS Code. As you have just installed it and VS Code doesn’t know about it.


For making it work in VS Code, you just have to do one last step and that is to set the path for MinGW in the environment variable so that the VS Code knows where to find the required C and C++ compilers when they are needed.


So, now you will have to locate the MinGW folder which will be created in the C Drive as we selected that as our drive to install.


MinGW folder


Here, go to the bin directory.


MinGW folder - bin folder


There you will find the gcc and g++ executable files so now copy the path for this bin folder i.e. C:\MinGW\bin


Now, open environment variables by going into system properties.


System Properties


Now, under user variables select Path variable and click on Edit.


environment variables


Now, click on New and paste the copied path for MinGW bin folder.


environment variables add mingw path


Click on OK and it's done.


You can check if it's working or not by opening the VS Code and run any C and C++ programs.


Note: Install Code Runner extension in VS Code for running programs.




1 comment: