Monday, 29 September 2014

Setting up gcc compiler in Windows Operating System

The biggest issue with Windows Users doing Coding for the first time is to find a reliable C Compiler for Windows. Though we have a number of compilers available for Windows, gcc compiler which is available in Unix based systems is the best C Compiler accepted unanimously worldwide.

Hence in this step by step tutorial I will try to help you install a gcc compiler on your Windows System.

Step 1 

Downloading and Installing MinGW and GCC
Go to the MinGW download site and download the file mingw-get-setup.exe. 

http://sourceforge.net/projects/mingw/files/Installer/

Run the downloaded file on your computer, accept all the default settings and continue. You should get the following screen:


Click on “Continue” and the MinGW Installation Manager will open and look like this:

Mark the following packages for installation
mingw-base
mingw-gcc-g++
Click on Installation->Apply Changes.
After the installation is complete click on “Close” and exit the MinGW Installation Manager.

Step 2

Download and Install Notepad++
Go to the Notepad++ download page:

http://notepad-plus-plus.org/download/v6.6.9.html

Download Notepad++ Installer.
Run the Installer and accept all the default options to install Notepad++.
You should now be able to run Notepad++ from the Start menu.

Step 3

Setting up the system for programming
First of all create new folder on your disk to keep all the programs separately. Lets say you create it in your C drive can call it “code”.
Since MinGW was installed using default options, it should be in the C:\MinGW folder.
We will now create a short batch file to setup the command prompt to work with MinGW.  
Open Notepad++, create a new file with just the following command:
PATH=C:\MinGW\bin;%PATH%
Save the file as “setup.bat” in the folder c:\code.

Alternatively Right click on My Computer and in the Environmental Variables copy C:\MinGW\bin; and add it to the Path. 

Now you are ready to edit and compile programs.

Step 4

How to edit and compile programs

Open Notepad++, write your program there and save it in the C:\code folder. Lets say the program is called “sample.c”.
Open the Windows Command Prompt by typing cmd in the Start --> Run. Change to the code folder by typing:
cd c:\code
Setup the command prompt using our batch file like this
C:\code>setup.bat
You can now compile your program like this:
C:\code>gcc sample.c
If there are errors, you will have to go back to Notepad++ to change the code.
If compilation succeeds, you will see the file a.exe created in the same folder.
Run the program like this:
C:\code>a.exe
By default every successful compilation will result in a.exe getting created and overwritten for every subsequent compile. If you want a different name for your compiled program, you should add the output file name to the gcc command like this:
C:\code>gcc -o sample.exe sample.c
And then run the program like this:
C:\code>sample.exe

Tips
You only need to run the setup.bat file once when you open the command prompt. After that the settings will stay until you close that command prompt.

That’s it !! You are ready to code.
Happy Coding.