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)
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
We will be installing GCC for Windows on our system.
-
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
-
Done? Great! Next, click on the downloaded installer. The following screen should appear.
- Click on "Create". The next screen that appears should be something like this -
- Select MinGW-w64 if you are using a 64-bit OS, else select MinGW (32 bit)
- Select the installation directory, and note it down somewhere. We will be needing it again.
-
From the above screen, select "TDM-GCC Recommended, All Packages" from the dropdown
-
๐ Press Install, and Step One is complete! Woohoo! ๐
Moving on to step two -
Why VSCode, you may ask. I have chosen VSCode for the setup, as it is very customisable, and has neat extensions for almost everything
-
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"
-
Run the downloaded installer. You should see a screen like follows -
-
Make sure to read the Agreement thoroughly. Cancel the installation if you find something that you do not agree with.
-
If you agree with what the Agreement says, select "I Agree", and press "Next"
-
Check all the boxes on the screen. It will save us some time later.
-
Press Next, Review your choices and then press Install!.๐ Step Two is now completed ๐
This step could be a bit big, depending on your choices.
-
Restart your computer. We want it prim and proper for this step.
-
Back already? Great!
-
Testing it out -
- Open a command prompt. If you dont know how to do that, no worries.
- Press the Windows key on your keyboard. It should be somewhere near your space bar key.
- Type in the box "cmd", and press "Enter"
- A magical black box should appear. That's your command prompt!
- Type the following command (i.e, type the text, and press Enter) -
gcc.exe
- You should get a similar output -
- Open a command prompt. If you dont know how to do that, no worries.
gcc: fatal error: no input files
compilation terminated.
4. If you get the above error, you are actually doing it right!
- Open Visual Studio Code. If you checked all the boxes in Step 2 - 5, you should have a desktop icon. Double click on that.
-
You should be able to see the above screen.
-
Next, open your workspace folder. To do so, go to
File -> Open Folder... (Ctrl + Shift + O)
-
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
)
-
The window should now reload, and your folder name will be visible in the titlebar. Excellent!
-
Step Three is done and dusted! Now, onto the final step -
- You should see an "EXPLORER" tab on the left hand side of the screen, like so -
-
Click on the small file icon with a plus
+
sign on it. In the above image, it is below the "View" Menubar. -
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
) -
Write your first lines of code in the opened file. For example -
#include <stdio.h>
int main(){
printf("Hello, World!");
return 0;
}
-
Next, we will compile your code.
-
Open a terminal window, either from the menu bar
Terminal -> New Terminal (Ctrl + Shift + ')
-
A black command prompt should appear below, as shown above.
-
Next, in the prompt, type
gcc.exe
followed by the file name For example -gcc.exe main.c
-
An
a.exe
file should appear in your explorer. We will run it via command prompt. -
Type
a.exe
in the command prompt. It should output -Hello, World!
- Success! We have finally set up our C and C++ development environment!
-
For C++, use
g++.exe
and notgcc.exe
-
Recommended extensions -
-
You may set up an automated build task using
Ctrl + Shift + B
- Reason for choosing MinGW + GCC + VSCode -
- Robust IDE, well supported, easy to navigate.
- Integrated terminal makes compilation and execution easy
- Decent autocomplete and code warnings, although it does require some configuration and extensions
- Common compiler, well supported, unlike non-standard compilers such as Turbo C / C++
- Easy compilation
- Light installation, unlike VS + cl.exe, which can go into 10s of Gigabytes.
Useful, thanks!
(currently need to help a friend install a C++ environment, and we're not able to setup a call)