12.22 – Creating executable file using gcc

by subbu on January 4, 2014

gcc  is the GNU implementation to C language. It is the default C compiler in Linux. It is available to windows users as MinGW gcc. It can be downloaded and installed as explained in the previous session 1.5 – How to install MinGW gcc in Windows

Creating an executable file using MinGW gcc in windows

Note: Before going further set path to c:\MinGW\bin to access gcc from any where.

Navigation:

  • Right click on MyComputer (Computer in case of Windows 7)
  • Select properties
  • [Select Advanced System Settings in case of Windows 7]
  • Select Advanced tab
  • Select Environment Variables           

Selecting environment variables

  • Select path under System variables
  • Select edit
  • Add c:\MinGW\bin;.; to the existed path value
  • Select OK

Now we can access gcc from any where

Setting path to gcc

Create or change to any working folder (d:\fox)

Navigation:

  • Go to the DOS shell by selecting start – run – type cmd – open
  • Change to d:
  • Create a folder fox using the command md (Making directory)
  • Change to fox folder using cd (Changing Directory)
c:\...\...>d:
d:\>md fox
d:\>cd fox
d:\fox>

Creating fox folder

Type the program in the text editor and save with .c extension

d:\fox>edit add.c
#include<stdio.h>
int main()
{
 int a,b,c;
 printf("Enter two numbers:\n");
 scanf("%d%d",&a,&b);
 c=a+b;
 printf("Sum of two numbers %d",c);
 getch();
 return 0;
}
  • Select File (Alt+F), Save (S) to save the program
  • Select File (Alt+F), Exit (X)

Compile the program using MinGW compiler (gcc), it will generate add.exe

d:\fox>gcc –o add  add.c

compiling using MinGW gcc

The executable file generated by the MinGW compiler (gcc) can be executed in two ways that are

1) By typing the name of executable file at command prompt

d:\fox>add
Enter two numbers:
12
34
Sum of two numbers 46

executing add

2) By double click on executable file in windows mode

Executing a program in windows1

Creating an executable file in Linux using gcc

Under Linux environment, executable file generated by the compiler (gcc) is saved with .out extension.

Type the program in the text editor gedit and save it with .c extension

$gedit add.c
#include<stdio.h>
int main()
{
 int a,b,c;
 printf("Enter two numbers:\n");
 scanf("%d%d",&a,&b);
 c=a+b;
 printf("Sum of two numbers %d",c);
 return 0;
}
  • Select File (Alt+F), Save (S) to save the program
  • Select File (Alt+F), Quit (Q) to come out of the text editor

Compile the program using gcc, it will generate add.out

$gcc –o add  add.c

The executable code generated by the compiler can be executed by typing the name of executable file at Linux shell

$./add
Enter two numbers:
12
34
Sum of two numbers 46
$

executing in linux

Previous post:

Next post: