Saturday 15 November 2014

How to configure gcc in Notepad++ for C11


Configuring notepad++ to compile and execute C programs using gcc:

Notepad++ is a free open source editor that supports several languages. It is popular for its user friendly, higher execution speed and less size. It is developed for windows environment in C++. It consumes lower memory and CPU power. It can be used to write, test and execute programs of different languages. Now we will see how notepad++ can be configured to compile and execute C programs using gcc.
Step1: Download and install the latest version of notepad++ from its official website www.notepad-plus-plus.org for free of cost
Step2: Download and install MinGw gcc as explained in the previous session.
Step3: Configure notepad++ for gcc

Creating C compiler tool in Notepad++

→ Open the notepad++
 Select Run – Run (F5)
creating tools in notepad++
→ Type the following compiler script in “The program to Run” panel
gcc -o "$(NAME_PART)" "$(FULL_CURRENT_PATH)"
gcc is to call compiler
-o is to create executable file with the file name
“$(NAME_PART)” to specify the file name without extension
“$(FULL_CURRENT_PATH)” to specify the current file and its path
For example if we save the file with “hello.c“, then the run command replaces compiler script withgcc -o hello c:\…\…\…\hello.c
Compiling tool in notepad++
 Select save to create short-cut to compiler tool (ctrl+f9)
creating compiler short-cut in notepad++

Create the C execution tool:

 Select Run – Run (F5)
 Type the C program execution script in “The program to Run” panel
"$(NAME_PART)"
Here “$(NAME_PART)” is to specify the file name without extension to call the executable file.
For example if we save the file with “hello.c” then the execution tool replaces the execution tool script with hello
 Select save to create short-cut to execution tool (ctrl+f5)
Creating C execution tool in Notepad++

Executing a C program in Notepad++:

Type the program
1
2
3
4
5
6
#include
int main()
{
  printf("Hello World");
  return 0;
}
C program in Notepad++
Use Ctrl + F9 to compile the program
Use Ctrl + F5 to execute the program
Executing a C program in Notepad++

How to install gcc in Windows


gcc installation on Windows:

gcc is a C compiler implemented by GNU. It is generally available with different flavors of Linux as an inbuilt tool. Now gcc is also available for Windows. MinGW is a project whose goal is to provide runtime, headers and libs to develop window applications using gcc. The official web site of Equation Solution www.equation.com provides gcc compiler for Windows XP (32bit) and Windows 7/8 (64bit) as separate builds. The latest versions of MinGW gcc implement ISO/IEC C11 standards.
Now we will see step by step procedure of installation and usage of MinGW gcc on Windows.
Installation:
Step1: open the site of “Equation Solutions” by clicking on the link ww.equation.com
Select Home – Programming tools – FORTRAN, C, C++ from Navigation Tree
download link of mingGW gcc
Step2: Go down to the page and select proper gcc build according to your platform that is gcc 32bit for Windows XP or gcc 64bit for Windows 7/8.
different builds of mingGW gcc
Step3: Create an installation folder (c:\MinGW)
Step4: Double click on downloaded “gcc setup executable file”
installation of gcc
Step5: Accept the agreement
gcc agrement
Step6: Select “Browse” to select installation folder (Example: c:\MinGW) and  Select “install” to proceed installation in the selected folder c:\MinGW
gcc installation
Step7: Select “finish” to complete installation
finishing installation

Executing C program using gcc:

Step1: Open the text editor “Notepad” by selecting start button, typing notepad in command text field and press enter. In case of Windows XP select start – select run – type notepad – select open
opening notepad in windows 7
Step2: Type the program in the text editor (notepad)
1
2
3
4
5
6
7
8
9
#include
void main()
{
int x,y,z;
x=10;
y=20;
z=x+y;
printf("Sum of two numbers: %d",z);
}
simple c program
Step3: Save the program with .c extension under a particular directory by selecting file – save. Here, I am saving under d:\demo as first.c
saving c program
Close the editor by selecting file – exit
Step4: Open the command prompt to compile and execute the program by selecting start button, typing cmd in command text field and press enter (¿). In case of Windows XP select start – select run – type cmd – select open.
opening command prompt in windows 7
Step5: Compile the program using the C compiler gcc. The compiler translates the source code and produces equalant executable code with the name a.exe (produces a.out in case of Linux). It can be executed by giving its name directly at the command prompt.
d:\>cd demo
d:\demo>gcc first.c
d:\demo>a
gcc on windows
Here there is a problem with the executable file a.exe because the output of gcc for every C program is a.exe. The executable code of current program overwrites the executable code of previous program compiled. If we want to execute the previous program then need to be recompiled.
We can produce different executable files with different names by sending parameter –o to gcc while compiling.
The general form of –o parameter is
Syntax of gcc
d:\>cd demo
d:\demo>gcc -o first first.c
d:\demo>first
gcc on windows
Here option –o generated the executable file with the name “first.exe” instead “a.exe”. It is executed by giving its name at terminal prompt. Like wise we can generate different executable files for different C programs.