How to Set up Visual Studio Code for C and C++ Programming

How to Set up Visual Studio Code for C and C++ Programming

Hello everyone in this article we will discuss C and C++ programming setup in visual studio code for windows, Linux, and macOS. C and C++ are high-level programming languages for building software, browsers, etc. It is usually recommended to use a code editor to compile them as they come with a lot of additional features. Visual studio code is one of the popular code editors among the developer community. In this article, we will show you how to install all the compilers(gcc, g++), and the extensions required step by step.

Visual studio code

If you are familiar with vs code I guess you don’t need any introduction. For those who are not familiar with VS code make sure you install it first. Microsoft developed VS code in 2015 since its release, it has become one of the most popular code editors. It has many other features like an integrated terminal, support for IntelliSense code completion, code snippets, git control, etc. Once you download it starts reading the docs to get familiar with the editor and their features.

Installing compilers

C and C++ have compiled languages. So we need to install the compilers to run the code. A compiler is a software used to translate your computer-written code into machine code. To run C and C++ we need to install the MinGW compiler. We need to install the gcc and g++ compiler and gdb debugger. The process is different for different operating systems let’s now discuss them in detail.

Windows

On Windows, we need to install several software to install the compilers. First, visit the official MinGW website and go to the download sections there click on MSYS2 then click on installation GitHub. Now it will redirect to its official MSYS2 website now scroll down there you can see the installer, download the .exe file and follow the instructions mentioned below.

MSYS is a software library you can download a lot of stuff using this software on windows. Here our goal is to install C and C++ compilers. Once the installation is done the terminal will open automatically if it isn’t click on Windows and search for MSYS2 UCRT64 and open it.

Firstly we will update the packages so that we can download the latest version of it. Make sure you have a stable internet connection before you start downloading packages. To start downloading enter the following command

$ pacman -SyuCode language: Bash (bash)

It will show some details about the size to proceed with installation enter y and hit the enter button it will start updating the packages. To complete the process enter y and hit the enter button the terminal will be closed. Now click on Windows and search for MSYS2 MSYS here we will update some base packages to do that enter the following command. And enter y and hit the enter button where ever required.

$ pacman -SuCode language: Bash (bash)

Now click on the windows button and search MSYS2 MINGW64 for 64-bit devices for 32-bit devices it is MSYS MINGW32 and opens it. Here we will install the gcc and g++ compilers. To do that enter the following commands

For 64 bit

$ pacman -S mingw-w64-x86_64-gccCode language: Bash (bash)

For 32 bit

pacman -S mingw-w64-i686-gccCode language: Bash (bash)

Enter y and hit the enter button to proceed with the installation. Once the installation is done check the version of gcc and g++ to do that enter the gcc --version g++ --version command and hit enter in your MSYS terminal, not in your system terminal. It will show the version if it shows any error do the above process again. As of now, the latest version is 12.2.0 it will be greater if you are seeing this in the future it will be a greater number than this.

Now we will install the debugger so that we can debug our code. To do that enter the following commands 

For 64 bit

$ pacman -S mingw-w64-x86_64-gdbCode language: Bash (bash)

For 32 bit

$ pacman -S mingw-w64-i686-gdbCode language: Bash (bash)

After the installation checks the version of gdb and enters the gdb --version in you are terminal. And one last thing we are going to do is set up the path environment variables. This is the most important step lot of people get stuck here so read it carefully. Now open your command prompt and check the version of gcc it is showing an error right you will get an error message that “gcc is not recognized as the internal or external command “ it is because we didn’t add gcc to the path variables. Close the command prompt.

Firstly we have to copy the path where the compilers have been installed. Open the file explorer we have installed compilers in the c drive open the msys64 folder there you can see mingw64 and mingw32 open them according to your device. Then open the bin folder and copy the path. To copy the path go to the navigation bar and left click now you can see click on copy address. It looks like this C:\msys64\mingw64\bin for 64-bit and for 32-bit it is C:\msys64\mingw32\bin 

Now go to your search bar and search environment variables and open it. Click on environmental variables now in system variables click on the path and click on edit. Now in the new window click on new and paste your path over there. Finally, click on ok on every new window that has opened during this process. That’s it hell of a process, isn’t it? To confirm this open your command prompt and enter the command gcc --version now you can see the version of the gcc. Read the extensions section before you start writing the code.

Linux

The process is very simple in Linux(Ubuntu). Firstly let’s update to the latest version 

$ sudo apt-get updateCode language: Bash (bash)

Enter your password. Once the update is done we need to install the compiler packages to do that enter the following command

$ sudo apt-get install build-essential gdb g++Code language: Bash (bash)

Once the installation is done check the version of gcc and gdb. And install the extensions mentioned in the extensions section to run your code

macOS

The process is very simple in macOS you just have to enter two commands in the terminal to download the MinGW compiler. Now open your terminal and paste the following command 

Intel Mac

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Code language: Bash (bash)

M1 Mac

arch -x86_64 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install)" < /dev/null 2> /dev/nullCode language: Bash (bash)

This command will install homebrew in your mac system. Now we will install the MinGW compiler 

Intel Mac

brew install MinGW-w64Code language: Bash (bash)

M1 Mac

arch -x86_64 brew install MinGW-w64Code language: Bash (bash)

Wait for the installation to complete. That’s it the MinGW compilers are installed in your system. Check the version of the compiler to that and enter the following command gcc --version. Install the extensions mentioned in the extensions section to run your programs.

Extensions

Once you installed the required compilers you need to install some extensions. You can find this extension on the left side of your interface just click on it or else click ctrl+shift+X on windows and Linux on the mac cmd+shift+X will open extensions. Now search for C/C++ and install it.

 
Another extension you need to install is code runner this extension will help you to run your code easily by clicking the run button. Once the installation is done restart the vs code and you are good to go now you can run your programs easily from vscode. Now there is one last thing we need to do. That is open your settings ctrl+, on windows and Linux on the mac cmd+,. Once you open the settings search for Run In Terminal in the search bar. And scroll down there you will find a Code-runner section tick the box.

Run a simple program to confirm if everything is working or not.

#include<iostream>
using namespace std;
 
int main() {
    cout<<"Welcome to codedamn!";
    return 0;
}
Code language: C++ (cpp)

Codedamn playground

As of now, you should have installed the compilers and be able to run the programs in the vs code. It’s a heck of a process, isn’t it? Especially for windows users, it will take a lot of effort and patience. Often when compiling your code you get a lot of errors like compilers not being installed properly or if you didn’t add them to your path the code won’t compile. And if you are using low-end devices becomes difficult to run your programs. To overcome these problems codedamn has come up with a new online C/C++ playground where you can write, edit and run your code. The big advantage is you can write code within your browser and no need to download any software all you need is a device with a good internet connection.

To use the playground go to your browser and search for codedamn playground open the first link. Signup to codedamn if you are new to codedamn. Now click on the C/C++ playground and create a new project. Now you can write your code and run it. This feature is also available for mobile devices just follow the above-mentioned steps. And more importantly, you can share your playground and collaborate with other developers\friends. You can use the terminal at the bottom to enter into interactive mode and run your code line by line. More importantly, you can save your code after you compiled it will be there in the browser until you delete it. There are other playgrounds as well totally free of cost check them out.

Conclusion

By now you must be able to run your C/C++ programs in the vs code. We have discussed all the software required to run the code. It’s a bit long process for windows compared to Linux and mac users. Make sure you add the compiler path to environmental variables in the windows systems you do not need to do that for Linux and mac. Make sure you install the extensions C/C++ and coderunner you need to install them inside the vs code it is mandatory for every OS. codedamn offers a free online C/C++ playground where you can write and run your code. That’s it from this article. If you have any doubts or queries comment below in the comment section. Check out other articles from codedamn about C/C++ language. Thank you!

Sharing is caring

Did you like what Thrishank wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: