VS Code is the most popular IDE to date today. It is the most flexible IDE with tons of customizable features. It is one of the most versatile development envrionments out today and can be configured and used with any other programming languages out there.
C++ is one of the most powerful programming languages out there. It is used to write computer programs ranging from simple Hello, World console applications to major operating systems and cloud infrastucture code. There are numerous programming languages itself that are built into C & C++ eg. Python, PHP etc. So learning C++ will build the concepts to work with any and every programming languages there are.
There are various online and offline IDEs for writing C or C++ code but a reliable code editor is important to have an efficient coding experience. In this post, you'll learn how to setup VS Code for writing C/C++ Code. There are other popular IDE's you can use such as CodeBlocks IDE. It simple and easy to use, has quite a lot many features and suitable for day to day C/C++ coding. Now for those who want C/C++ experience in VS Code IDE.
In order to successfully complete the setup process we must exactly need follow the steps. Take a look below.
VS Code is all you know is powerful, flexible and multi-platform code editor. We are going to use it as our C/C++ IDE so a little configurations would be required. So first of all Download VS Code from the official website and follow the installation process. A few clicks would get you up and running with visual studio code.
After you've successfully installed VS Code, we'd need to add a few extension to enhance C/C++ suppport in VS Code. First we'll install the C/C++ for Visual Studio Code extension that adds extra enhancement and support for C/C++ development. Click open the Extension Manager on the VS Code sidebar or use the shortcut Ctrl+Shift+X. Now search for the extension C/C++ for Visual Studio Code and install it.
Now, again search the package manager and this time look for an extension named Code Runner and install it. This extension will help you run C/C++ code with a click of a button .
Now, if you have successfully installed the VS Code IDE, it's now time to install the GCC C++ Compiler and GDB debugger from Mingw-x64 as we'll use this complier with our VS Code IDE to create programs that runs on the Windows Operating System. Once, we complete the entire setup, we'll test it by building a basic Hello, World!! program in C++.
Visit MSYS.org and Download the MSYS2 installer. MSYS2 is a collection of tools we can use to build, install and run windows applications. After we install the tools, we'll be downoading the C++ compiler using the package manager called Pacman which is the part of the tools we install. Lets follow. Once you download the installer and run it you'll see a dialog as shown below.
Now, when you click next it will ask you to select a default installation directory. You can leave the one default that is C:\msys64 and proceed further.
Click next and Let the installation process continue further.
Once the installation is complete you should see a screen like below (if you selected "Run MSYS2 now") or you may click on the Start button and search for MSYS2 MinGW x64 and launch it.
It's now time to run some commands, first we'd sync the package repository of the package manager Pacman we talked about earlier. Then we'll fetch and install the requisite tools required for our development environment. Copy the command below write it or paste it by right clicking on the dark window that you see.
You'll see that the package manager starts syncing the repository.
Now you may update rest of the base packages by running the following commands again in the same terminal.
Once these steps are complete, we're now ready to install the final package that is our MinGW GCC Compiler. Run the following command on the same terminal.
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
After a few seconds it will show you the package size and ask you to continue further with the installation by pressing "y "
After a few minutes, the installation process for the compiler will be finished and if you now browse the C:\msys64\mingw64\bin directory, you'll be able to see the compiler and related tools and that's it for this part.
Now that we've installed the compiler and related tools with it, it is important to add their directory to the system's PATH Variable so that we can access the compiler right from our terminal. You can follow the steps below to add the path to your Mingw-w64 bin folder to the Windows PATH environment variable.
Now, after updating the path variable and if you open a console, CMD and type
gcc --version, you'd see something like this
Please make sure to restart VS Code or any other console window for PATH update to take place.
Now that we have successfully completed the set up process for C/C++ Development environment on VS Code, we can now create and run C/C++ programs right through VS Code IDE. It's now time to test the setup and there's no better way than writing a Hello World! program. Follow the tutorial below.