I am writing this article because I had faced several issues while running the C++ code in VS Code. As usual, everyone was using the CodeBlocks IDE and DevC++ and many more. But I was already used to Visual Studio Code for all my programming stuff. In this article, I’ll show you how to set up your compiler in VsCode. I will be using a Windows OS throughout this article.
So let’s Start..
- Prior knowledge of Programming.
- Visual Studio Code Editor ( Download from here)
- Good Internet Connection !!
Download and install a C++ compiler:
After downloading, install MinGW and wait for the “MinGW Installation Manager” to show up.
When the “MinGW Installation Manager” shows up, click on
mingw32-gcc-g++ then select “Mark for Installation”.
In the menu at the top left corner, click on “Installation > Apply Changes”
Wait and allow to install completely. Ensure you have a stable internet connection during this process.
Edit your PATH environment variable to include the directory where the C++ compiler is located :
After installing MinGW, it can be found in
C:\MinGW\bin. Now you have to include this directory in your environment variable PATH. If you've been using computers for a while now you should know how to do this already, but if you don't, here are a few resources:
Install Code Runner extension in VS Code :
Now we are done with our compiler set up, let’s install Code Runner
Search in VsCode marketplace tab:
After installing restart VsCode. Open your C++ file in VsCode.
Here’s a basic hello world program below:
Run your code using Code Runner:
1.Use the shortcut
2. Press F1 and then select/type Run Code
3. Right-click the Text Editor and then click Run Code in the editor context menu.
The code will run and the output will be shown in the Output Window. Open the output window with `Ctrl+ shortcut.
DO NOT SKIP THIS STEP :
By default, VsCode’s output terminal is read-only. It means that You can’t use terminal to take input by default. To fix this, you need to manually enable read-write. Steps as follow:
- In VsCode, Go to File > Preference > Setting.
- In the User tab on the left panel, find the extensions section
- Scroll and find ‘Run Code Configuration’
- Scroll and find a checkbox
Run in Terminal(Whether to run code in Integrated Terminal) Check the box.
- In your
Well,you’re done and ready to roll !!! Happy Coding !!