Compiling C program and creating executable file in Linux

For compiling a c program you need GNU C compiler, also for creating an executable of this C program you need C compiler. Mostly C programs are compiled using cc command but gcc command can also be used to compile C programs. Below is mentioned the procedure to install C compiler in your machine if it is not installed in case.

  • Verifying whether gcc is installed or not: use this command
which gcc

Output:

/usr/bin/gcc
  • Knowing the version of gcc if it is installed:
$ gcc --version

Output:

gcc (GCC) 4.0.3 20060212 (prerelease) (Debian 4.0.2-9)
Copyright (C) 2006 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 a C program Use the below command syntax:

gcc prog_name.c -o prog_output_name

  • Now you need a c program. Lets write this basic hello world c program.
  • Use a text editor such as vi or gedit to create a C program called first.c:
$ vi first.c
  • Copy paste the following lines of code:
#include <stdio.h>
int main(void){
printf("My first C programn");
return 0;
}
  • Now lets compile this c program named first.c and create a executable file of this program named first:
$ gcc first.c -o first

OR

$ cc first.c -o first

  • Lets run this program:
$ ./first

Output:

My first C program
This entry was posted in C Programming and tagged . Bookmark the permalink.