Library Technology - Reviews, Tips, Giveaways, Freeware

Library Technology – Reviews, Tips, Giveaways, Freeware

Linux tips: How to compile & execute C programs under Linux (Absolute basics)

Posted In Linux - By Techtiplib on Monday, June 18th, 2012 With No Comments »

This article is for those guys who used to write a lot of programs under Windows.. and now have entered the Linux territory. You have probably heard a lot about Linux and how you can do some real good programming under Linux. But right now you cant even get the simplest of Hello World programs to compile. 

Here’s how you do it.


You can type you C program using any of the editors that are available under Linux such as vi or emacs or any other editor. My favourite is Emacs and I have explained how to get started with Emacs in Scheduling tasks using Cron – Part I.

Once you have written and saved your C program using any editor return to the prompt. An ls command should 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 hello secondprogram.c

Would create an executable by the name hello for your source code named secondprogram.c

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

$ ./firstprogram

$ ./hello

Or whatever you named your executable. 

This is the absolute basics of compiling C programs under Linux. Watch out for the articles that shall explain multiple file program compilation using make and other such tools.


More contents in: ,

About - Hey, this blog belongs to me! I am the founder of TechTipLib and managing editor right now. And I love to hear what do you think about this article, leave comment below! Thank you so much...