Skip to content

Instantly share code, notes, and snippets.

@arnitdo
Last active April 24, 2021 18:28
Show Gist options
  • Save arnitdo/039c36d3d44c8dae239a615c33cb0242 to your computer and use it in GitHub Desktop.
Save arnitdo/039c36d3d44c8dae239a615c33cb0242 to your computer and use it in GitHub Desktop.

1. What is a compiler?

A compiler is a computer program that converts code written in one language, to another. We will be using a C / C++ compiler called GCC.

Other alternatives are -

1. Clang / LLVM

2. Visual C / C++ compiler (Included in Visual Studio, NOT VISUAL STUDIO CODE)

2. What is an IDE?

IDE Stands for Integrated Development Environment. It is a set of tools that helps us write, edit, refactor and debug our code. Some IDEs also come with features such as Version Control, Dependency Controls, etc. We will be using Visual Studio Code, commonly called VSCode.

Other alternatives are -

1. Sublime Text 3

2. Visual Studio (If you are planning on using VS, the below steps DO NOT apply.)

3. Atom

C and C++ Setup On Windows

Step 1 : Installing a compiler

We will be installing GCC for Windows on our system.

  1. Download the TDMGCC Installer from here - https://github.com/jmeubank/tdm-gcc/releases/download/v9.2.0-tdm64-1/tdm64-gcc-9.2.0.exe

  2. Done? Great! Next, click on the downloaded installer. The following screen should appear.

TDM-GCC Screen

  1. Click on "Create". The next screen that appears should be something like this -

TDM-GCC Screen 2

  1. Select MinGW-w64 if you are using a 64-bit OS, else select MinGW (32 bit)

TDM-GCC Screen 3

  1. Select the installation directory, and note it down somewhere. We will be needing it again.

TDM-GCC Screen 4

  1. From the above screen, select "TDM-GCC Recommended, All Packages" from the dropdown

  2. ๐ŸŽŠ Press Install, and Step One is complete! Woohoo! ๐ŸŽŠ

Moving on to step two -

Step Two - Installing Visual Studio Code

Why VSCode, you may ask. I have chosen VSCode for the setup, as it is very customisable, and has neat extensions for almost everything

  1. Download the Visual Studio Code Setup from here - https://code.visualstudio.com/download . Be sure to select the big blue button that says "Windows 7, 8, 10"

  2. Run the downloaded installer. You should see a screen like follows -

VSCode Setup - Screen 1

  1. Make sure to read the Agreement thoroughly. Cancel the installation if you find something that you do not agree with.

  2. If you agree with what the Agreement says, select "I Agree", and press "Next"

VSCode Setup - Screen 2

  1. Check all the boxes on the screen. It will save us some time later.

  2. Press Next, Review your choices and then press Install!.๐ŸŽ‰ Step Two is now completed ๐ŸŽ‰

Step 3 : Getting your development environment ready

This step could be a bit big, depending on your choices.

  1. Restart your computer. We want it prim and proper for this step.

  2. Back already? Great!

  3. Testing it out -

    1. Open a command prompt. If you dont know how to do that, no worries.
      1. Press the Windows key on your keyboard. It should be somewhere near your space bar key.
      2. Type in the box "cmd", and press "Enter"
      3. A magical black box should appear. That's your command prompt!
    2. Type the following command (i.e, type the text, and press Enter) - gcc.exe
    3. You should get a similar output -
gcc: fatal error: no input files
compilation terminated.
4. If you get the above error, you are actually doing it right!
  1. Open Visual Studio Code. If you checked all the boxes in Step 2 - 5, you should have a desktop icon. Double click on that.

VSCode Interface - Screen 1

  1. You should be able to see the above screen.

  2. Next, open your workspace folder. To do so, go to File -> Open Folder... (Ctrl + Shift + O)

  3. If you do not have a workspace folder, create one from your file manager / explorer

(Note that by workspace folder, I mean the folder in which you will be storing your programs, and not a folder named workspace)

  1. The window should now reload, and your folder name will be visible in the titlebar. Excellent!

  2. Step Three is done and dusted! Now, onto the final step -

Step 4 - Making our very first program! Hype!

  1. You should see an "EXPLORER" tab on the left hand side of the screen, like so -

Programming - Screen 1

  1. Click on the small file icon with a plus + sign on it. In the above image, it is below the "View" Menubar.

  2. A text box should appear in the explorer tab. Write the name of your file in it, and press Enter. ( For reference, I shall be naming it main.c )

  3. Write your first lines of code in the opened file. For example -

#include <stdio.h>

int main(){
	printf("Hello, World!");
	return 0;
}
  1. Next, we will compile your code.

  2. Open a terminal window, either from the menu bar Terminal -> New Terminal (Ctrl + Shift + ')

Programming - Screen 2

  1. A black command prompt should appear below, as shown above.

  2. Next, in the prompt, type gcc.exe followed by the file name For example - gcc.exe main.c

  3. An a.exe file should appear in your explorer. We will run it via command prompt.

  4. Type a.exe in the command prompt. It should output - Hello, World!

Programming - Screen 3

  1. Success! We have finally set up our C and C++ development environment!

Notes :

  1. For C++, use g++.exe and not gcc.exe

  2. Recommended extensions -

    1. VSCode C/C++ Tools
    2. exe Runner
  3. You may set up an automated build task using Ctrl + Shift + B

Comments -

  1. Reason for choosing MinGW + GCC + VSCode -

VSCode -

  1. Robust IDE, well supported, easy to navigate.
  2. Integrated terminal makes compilation and execution easy
  3. Decent autocomplete and code warnings, although it does require some configuration and extensions

MinGW + GCC

  1. Common compiler, well supported, unlike non-standard compilers such as Turbo C / C++
  2. Easy compilation
  3. Light installation, unlike VS + cl.exe, which can go into 10s of Gigabytes.
@eccentricOrange
Copy link

Useful, thanks!

(currently need to help a friend install a C++ environment, and we're not able to setup a call)

@GauravVaria
Copy link

Thank you ๐Ÿฅณ ๐Ÿ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment