How to install C and C++ in VS Code ?
In this article we will show you how to install c and c++ in VS Code. VS Code is one of the most popular code editors out there and you can configure VS Code to work with any language. First we will be installing the C compiler and then we will configure VS Code to work with C and C++.
In our previous article we showed you how to install C and C++ using CodeBlocks. If you have already followed that tutorial then the C/C++ compiler is already installed on your machine and you can skip the installation of C and C++ compiler mentioned in this tutorial. You can directly go to the installation of VS Code.
Installation of C and C++ Compiler
First open this link and scroll down till you reach here.
Download the highlighted file x86_64-posix-seh by clicking on it. Once you click on it the download will automatically start. After the file is downloaded, extract the file.
To extract the file you need to have 7zip or Winrar installed on your computer. I will show you how to extract the file we downloaded using 7zip.
If you don’t have 7zip installed , download it from here.
You need to download the 64bit x64 version for windows.
Once 7zip is downloaded , open it and click on install.
It will install 7zip on your computer. Once 7zip is installed, we can use it to install our c compiler that we downloaded earlier.
For that right click on the x86_64-posix-seh file you download earlier.
If you are using windows 11, you need to right click on the file and select show more options, 7zip, extract to x86_64-8.1.0-release-posix-seh-rt_v6-rev0 .
If you are using windows 10 or below, you need to right click on the file, select 7zip, extract to x86_64-8.1.0-release-posix-seh-rt_v6-rev0
After extraction is completed, you will see a folder named x86_64-8.1.0-release-posix-seh-rt_v6-rev0 and inside that folder there will be a folder named mingw64. Copy the mingw64 folder to your C drive.
Now open the mingw64 folder that you copied to the c drive and copy it’s path.
If you copied the folder to your C drive, then the path will be C:\mingw64 . Now the final step of the C and C++ installation is to add this path to the system environment variables.
For that just open start and search for env.
Here choose edit system environment variables for your account option.
When you click on that , a window called system properties will open , there click on Environment Variables.
There choose Path under System variables and click on edit.
Click on new and add C:\mingw64 and C:\mingw64\bin to that list.
Just look at the highlighted portion of the image. You should add C:\mingw64 and C:\mingw64\bin like that to the list. Your list will look slightly different than mine. After you have added both to path , click on ok and you can close the window.
Now to check if it is working , open command prompt and type gcc -version and press enter.
Now if you see something like this , then you have properly installed C and C++ compiler on your pc. If you see any errors , leave a comment below and I will help you out.
We can move on to installing Visual Studio Code now.
Installing VS Code
Download VS Code from here.
Once it is downloaded, run the installer and follow the on screen instructions to install VS Code.
Once VS Code is installed, open VS Code and go to extensions.
In extensions search for C++ and install the following extensions:
- C/C++
- C/C++ Extension Pack
- C/C++ Themes
Now search for Coderunner in extensions and install Code Runner by Jun Han.
Now after that go to file -> preferences -> settings of VS Code.
In settings , go to run code configuration.
Now in Run Code configuration , scroll down and tick the checkboxes Run in Terminal, Save All Files Before Run and Save File Before Run.
That’s it , we have successfully configured VS Code with C and C++.
Now let’s run a sample helloworld program to test our installation.
#include <iostream>
using namespace std;
int main(){
cout << "Helloworld!!!" << endl;
return 0;
}
To run the code, click on the run button on the right side of the tool bar and you will see the output in the integrated terminal.
Now you can make C and C++ programs using VS Code.