Setup for coding C/C++ in VS CODE.

Setup for coding C/C++ in VS CODE.

The C compiler:

The computer can’t understand the code we write, so we need to convert it into machine language through a compilation process, and the program required for the task is a compiler. The compiler’s job is to check for any syntactic error, allocate memory, and convert the source code into a low-level language that the machine can understand. To learn more about the compiler and how it differs from the interpreter, click here.

The GNU C/C++ plus compiler is the most frequently used and freely available compiler is the GNU C/C++ plus compiler, which will be installed in this tutorial.

Almost all Unix-like systems (MacBook, ubuntu, etc.) have GCC compiler pre-installed. So, no need to install again. You can confirm it by typing gcc -v in command line.

In Microsoft Windows.

First, check whether your computer is a 64/32-bit computer.

Note:

You can install a 32bit compiler in a 64-bit system also. But the memory allocation will be different when you compare it with that of a 64-bit compiler. See the below image.

image.png

Procedure to install a 32-bit compiler:

1)Go to this link sourceforge.net/projects/mingw and download

image.png

2)After downloading, open it and click on install, then click on continue.

3)Now, wait until it downloads all the files. Then click on continue.

image.png

4)Click on the package named Mingw32-base, click on the mark for installation and repeat the same with Mingw32-gcc-g++.

5)click on the Installation tab (at the top left corner of the dialog box). Click on apply changes.

image.png

6)Click on Apply. Then you will see this.

image.png

7)After the download is complete, you will see this. Click on close.

image.png

Setting environment variables:

  1. Open windows explorer and go to local disk C

  2. Now, open the Mingw folder, open the bin folder, and copy the directory path, as shown below.

image.png

  1. After copying the directory path, Right Click on This PC -> Select/ Click on the Properties. Now scroll down until you find Advanced system settings and click on it. Then you should see a window like below.

image.png

  1. Now click on Environment Variables. Click on the path and edit.

image.png

  1. Click on new and paste.

  2. Now open windows explorer, open local disk C, open MinGW folder, open mingw32 folder, open bin folder. Copy the path and repeat step 5.

image.png

  1. Now click on OK, OK again.
  1. Now open the command prompt (type cmd in the search bar in the lower-left corner). Now type gcc --version and press enter.

image.png

  1. If you have followed the above steps correctly, then you should get the version information of gcc. (in my case it is 11.2.0)

    image.png

  2. If you get “gcc is not recognized as an internal or external command,” then restart your pc/laptop and repeat step 8.

  1. If you get the same error or any other error, Go to Local Disk-C or where you installed MinGW, delete the MinGW folder. Start from step 2. This should fix all problems

Note:

If problem persists, then comment below. I will add a fix for it at the end of the article. So, that it will be useful for others also.

For a 64 bit compiler:

  1. Download the installer github.com/msys2/msys2-installer/releases/d..
  2. Run the installer. MSYS2 requires 64 bit Windows 7 or newer.
  3. Enter your desired Installation Folder

image.png

  1. When done, tick Run MSYS2 now.

image.png

  1. Update the package database and base packages. Unless your setup file is very recent, it will take two steps. First run pacman -Syu.

image.png

  1. Run "MSYS2 MSYS" from Start menu. Update the rest of the base packages with pacman -Su
  2. Now MSYS2 is ready for you. You will probably want to install some tools and the mingw-w64 GCC to start compiling: Type: pacman -S --needed base-devel mingw-w64-x86_64-toolchain

image.png

Setting up VS code:

  1. Open VS code.

Note:

You can open vscode by typing code in command line(cmd in windows.)

  1. Then go to extensions or hold ctrl+shift+x.

  2. Then install C/C++ extension pack. image.png

Hooray!!

You are good to go !!!

Credits:

msys2.org

Article still in beta. : )