When I started my programming journey, the first language that I learned was C++. C++/C should be your pedestal to enter the world of programming. In this post, we are going to learn how you can configure VS Code for Microsoft C++ as it can be a little hassle as C++ needs a compiler and interpreter to run your code.
How to configure VS Code for Microsoft C++
To configure VS Code for Microsoft C++, follow the steps mentioned below.
- Download and install Microsoft Visual Studio Code
- Install C++ Extension
- Install and setup the MinGW Compiler
- Set the path and configure the Environmental Variables
Let us talk about them in detail.
1] Download and install Microsoft Visual Studio Code
First of all, you need to download VS Code. For that, go to code.visualstudio.com and download the application for your operating system. Now, run the installer and tick Add to PATH (requires shell restart) when asked to Select Additional Tasks.
2] Install C++ Extension
Now that we have the Visual Studio Code installed on our computer, it’s time to get the correct extension that will tell Visual Studio Code, “Hey this is a C++ code!”.
To do the same, launch VS Code, click on the Extensions icon or just hit Ctrl + Shift + X. Now, type “C/C++” in the given field and it will search for the extension in question. Click on the C/C++ extension and click on Install.
After installing the C/C++ extension, we are required to install Code Runner. This extension will allow us to run our code snippet or code file. We are not done yet, we need a compiler.
3] Install and setup the MinGW Compiler
Our computer systems are built using electronic switches and cables that can only function with binary digits 1 and 0. To make your high-level C++ code understandable by the computer’s central processing unit (CPU), you require a compiler that can translate it into machine language. For that very reason, we will be installing and configuring the MinGW Compiler.
First of all, let us download the MinGW Compiler from sourceforge.net. You can get the compiler for free. After MinGW setup is installed, go to your Download folder, run the setup, and follow the steps mentioned below.
- You need to follow the on-screen instructions to complete the installation process.
- Once installed, click on Continue.
- This will launch MinGW Installation Manager, where you need to right-click on the options and select Mark for Installation. Ideally, you only require a C++ Compiler, but we want you to be future-proof, hence, it’s better to install all the required compilers.
- After checking all the boxes, click on Installation from the top-left corner of the screen and select Apply Changes > Apply.
- Finally, let the step do its job and install all those packages we just ticked.
Once MinGW packages are installed, don’t just straight away start coding, we need to set the set and configure the Environmental variables.
4] Set the path and configure the Environmental Variables
Next, we need to set the path and configure the Environmental Variables. To do the same, first of all, open File Explorer, and go to Local Disk C > Mingw > Bin. And then copy the location, it should be something like C:\MinGW\bin. Make sure to paste it somewhere accessible.
To configure the Environmental Variable, search, and open “View Advanced System Settings”. In the Advanced tab, click on Environmental Variables. You then have to click on Path from the System Variables section and then click on Edit. Then click on New, enter the path we asked you to copy earlier and finally click on Ok. Finally, click on Ok and close the System Properties window.
This way, we have configured the Environmental Variable for our Compiler. To confirm it, open Command Prompt as an administrator and then run the following command.
If you get the following message or something similar, you are good to go.
g++ (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
Finally, you can start coding.
How do I run C++ in Microsoft Visual code?
To run C++ in Microsoft Visual Code, you need to install the C++ Extension and other essential extensions, then download and configure the compiler, you can use Clang or MinGW Compiler. We have aforementioned a detailed guide on how to install C++ and configure the compiler.
How can I set up a default code for C++ in Visual Studio Code?
To set up a default code in VS Code, first, open the Command Palette by pressing Ctrl + Shift + P. Then, search for “>Configure User Snippets” and click on it. Next, search for “cpp.json” and open the file. Finally, replace the comments in the file with the snippet code that you have copied to your clipboard.
Also Read: Best Free C++ IDE for Windows.