Search This Blog

Wednesday, August 28, 2013

compiling c program-linux

Compiling a c program in linux:

to run a c program in your linux machine you need 2 software packages

1.vi or vim editor
2.gcc compiler


check those software by typing the gcc and vi or vim command on the terminal
if it shows command not found then you need to install that software

to do so a simple command is useful for you

yum install software

yum command automatically searches the software package and installs the latest one

so type
yum install gcc vi vim

the it automatically install the 3 packages

vi and vim both are code editors vi is a default one but it show the code in single color
where as vim show the different colors .like variables,strings,functions...etc

its look like



see the colors in vim editor






after installing the packages first create  c file uisng vim editor

vim hw.c

its creats hw.c file and enter into editor now editor is in command mode so you need to enter into insert mode to edit the code for the type 'i'
the edit the code

the press 'esc' key to enter into command mode the ':x' to save and exit

* :x<Return> quit vi, writing out modified file to file named in original invocation
  :wq<Return> quit vi, writing out modified file to file named in original invocation
  :q<Return> quit (or exit) vi
* :q!<Return> quit vi even though latest changes have not been saved for this vi call


after exiting the editor compile the code using gcc compiler

gcc hw.c------------by default it outputs the file with a.out name

gcc hw.c -o hw.o----now the output fie name is hw.o

if there are no errors it will creates the output file
to run that file type

./a.out   or   ./hw.o

then your code will shows the output..


No comments:

Post a Comment