Thursday, June 16, 2011

How to Compile C in Linux

0 comments


How to Compile C program in Linux/Unix ?:

Some people have doubts about how to compile C source code in Linux/Unix.Firstly you need GNU project C and C++ compiler for compiling C program and create executable(EXE) file.Most Linux and Unix user start compiling their C program by the name cc.But you can use gcc command to compile program.


  • First make sure you have gcc installed.
Type the following Command in Terminal to verify that gcc is installed:
which gcc

Output:
/usr/bin/gcc
  •   Find out version of gcc:
gcc --version

Output:
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE

  • To compile C program you need to use syntax as follows:
gcc program.c -o program-output

Use text editor such as vi or gedit to creat a c program called sample.c
For example open gedit and type your code and save as sample.c


Type the following code:
#include<stdio.h>
int main(void){
printf("Hello! World.This is my sample program in C\n");
return 0;
}

How do compile my C program?:

  • To compile my sample.c program:
gcc sample.c -o sample

  • To execute C program:
./sample






Leave a Reply