In this blog we will see how we can:
- Install updated version of C/C++ Compiler using MSYS2
- Install Debugger
- Install Visual Studio Code (VSC) IDE
- Integrate GCC compiler and debugger with Visual Studio Code (VSC)
All steps are shown in this YouTube video.
The GCC Compiler and MinGW:
The C and C++ compiler is the GCC Compiler. To install GCC, we can install MinGW. MinGW stands for Minimalist GNU for Windows. It’s a free and open-source software development environment specifically designed to create native Microsoft Windows applications.
So, we will install MinGW that has the GCC compiler as part of it. We can install MinGW directly from the website but we will install this using MSYS2. MSYS2 is a software installation and building platform for Windows.
Why to use MSYS2 to install MinGW?
In simple words it makes the installation process simple and handles the dependencies and updates with no effort. It uses the package manager named as pacman.
- MSYS2 uses Pacman, a powerful package manager, to install MinGW and its dependencies effortlessly
- MSYS2 handles dependencies automatically, ensuring that all required packages are installed correctly.
To install MSYS2, go to this link:
- Under the “Installation” section, click the Installer to download that.
- Follow the steps for the installation process.
- It will be installed in C drive C:\msys64
MSYS2 provides a command-based interface. When you run the installed program it will open the command window something like this:
The first command will be using pacman to update the System. This will update system’s package database and upgrade all out-of-date packages. Use this command:
$ pacman -Syu
It will ask for yes or no as [Y/n], then press Y.
Now to install MinGW for 64 Bit system, use this command:
$ pacman -S mingw-w64-x86_64-gcc
It will ask for yes or no as [Y/n], then press Y.
After this step MinGW will be installed. If there is any error at the end, you may need to repeat the process.
Verify if MinGW and GCC Compiler has been installed:
First exit from the MSYS2 command window. For that, simply use the Exit command to exit from MSYS2 as:
$ exit
Now from the Windows start menu, search for MSYS2 MinGW 64. If you find that it means MinGW has been installed. Click that to open the MSYS2 command window again. We can verify if GCC compiler is installed successfully. For that, use the command:
$ gcc –version
And we should get the information about GCC with version number
Installation of GDB Debugger:
While coding in C and C++ we should also install the debugger which is a powerful tool to find and fix errors in the code. We will install GDB which stands for GNU Debugger.
For that, we will use this command in MSYS2 command window:
$ pacman -S mingw-w64-x86_64-gdb
It will ask for yes or no as [Y/n], then press Y.
To verify if the debugger has been installed successfully, use the following command:
$ gdb –version
You must see the version information in the output. Everything has been installed and we can simply exit the MSYS2 using the exit command.
Setting up Windows System Variables:
There is one last crucial step before we can start writing code, and that is making the GCC compiler accessible throughout your system. This means you can use the GCC compiler from any directory without specifying its full path. For that we need to open the command prompt of windows by going to Windows Search bar and typing CMD. It will open the command prompt as:
To verify if GCC is accessible here, write the command
> gcc –version
Most probably you will get the error like:
‘gcc’ is not recognized as an internal or external command,
The reason is that MSYS2 was installed in the Drive C. Locate that folder:
Inside msys64 folder, we have the folder for mingw64 –> bin and inside bin folder we have all exe applications like gcc and gdb. So we need to tell the Windows about this path so that when we will run the codes, it knows where to find the compiler GCC. Copy the path: C:\msys64\mingw64\bin.
Now go to Windows Search bar and search for “Environment Variables” and click “Edit the system environment variables”. This window will open:
Click “Environment Variables” at the bottom of the above window. A new window will open and we need to edit the path variable in the lower section:
Select the Path variable and Click “Edit” to edit it. This window will open:
Here click “New” to add a new path and paste the path C:\msys64\mingw64\bin. Then Click “OK” a couple of times.
Now you can open the command prompt again (using CMD in Windows search bar) and verify the installation of GCC and GDB with these commands:
> gcc –version
And
>> gdb –version
And this time instead of error you should get the version information.
C/C++ compiler and debugger has been installed.
Installation of Visual Studio Code and integrating GCC:
For the better and professional experience we can install the IDE “Visual Studio Code” and configure it for C and C++. This offers a rich set of features for code editing, debugging, IntelliSense, and project management, significantly enhancing productivity and code quality.
Let’s see how we can install and configure Visual Studio Code.
Go to this link: https://code.visualstudio.com/ And click “Download for Windows”
Choose the operating system as Windows 10 and 11 and installer download will start. Run the installer and follow the steps to install Visual Studio Code.
To install C/C++ support, go to Extension and then search for C/C++. And install the first extension you see.
You should also install another extension “Code Runner” which offers a bunch of useful features. The detail is provided in THISSSSSSSSSSSSSSSSSSSSS video.
Test C and C++ Codes:
To finally verify the execution of C and C++ programs, open some directory in Visual Studio Code, where you want to save the coding files. Create a new file for the C program and use the extension C (e.g. test.c). Test this simple Hello World program:
#include <stdio.h>
int main() {
printf(“Hello, World!\n”);
return 0;
}
You should see the output as the text Hello World.
Test another program that takes user’s input:
#include <stdio.h>
int main() {
int num;
// Prompt the user for input
printf(“Enter an integer: “);
// Read the integer input from the user
scanf(“%d”, &num);
// Calculate the square of the input number
int square = num * num;
// Print the result
printf(“The square of %d is %d\n”, num, square);
return 0;
}
You might face a problem taking input from the user. The solution is running the code in the terminal and that’s where the extension “Code Runner” we installed will help us. For that go to settings and search for “Code Runner”. Then search for the setting “Run in Terminal” and click the checkbox. Now the above code that takes input a number from the user should work correctly.
The last thing we will see is running a C++ code. No setting or configuration needs to be changed. You just need to use the extension .cpp instead of .c for the file. You can test this sample C++ program:
#include <iostream>
int main() {
int num;
// Prompt the user for input
std::cout << “Enter an integer: “;
// Read the integer input from the user
std::cin >> num;
// Calculate the square of the input number
int square = num * num;
// Print the result
std::cout << “The square of ” << num << ” is ” << square << std::endl;
return 0;
}
All steps are shown in this YouTube video.
Pingback: Install Icarus Verilog with Visual Studio Code: Beginner's Guide