Flipkart.com

Tuesday 18 September 2012

How to Compile & Execute C Program under Linux (Basics)

This article is for those guys who used to write a lot of programs under Windows & now have entered the Linux territory. You may probably heard a lot about programming in Linux. But you don't know how to compile it and run your code as it does not provide any interactive interface to do so.

Here's how you do it -



Procedure :

You can type you C program using any of the editors that are available under Linux such as vi or any other editor.
Once you are finished writing, save your C program. Now open a terminal and move to the location where you have saved for C program source code. Type ls (Listing Files). It will display your C program. It should have the .c extension. Now at the prompt type the following.

$ gcc -o firstprogram firstprogram.c

If your file is named firstprogram.c then type '-o firstprogram' as the parameter to gcc. This is basically your suggested name for the executable file that gcc would create. In case you typed something like the following

$ gcc firstprogram.c

You would be having a a.out in the same directory as the source C file. This is the default name of the executable that gcc creates. This would create problems when you compile many programs in one directory. So you override this with the -o option followed by the name of the executable

$ gcc -o xyz firstprogram.c

Would create an executable by the name xyz for your source code named firstprogram.c

Running the executable that you created is as simple as typing the following at the prompt.

$ ./firstprogram

OR

$ ./xyz

Or whatever you named your executable.

This is the basics of compiling C programs under Linux.


If you like the post please do subscribe us on Facebook, and on twitter.
Please share the post to support us.

0 comments:

Post a Comment

IHT