Jump to content

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

Guest ajaX

Recommended Posts

Guest ajaX

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 -



Procedure :


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 Article No. 12


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.


Tip brought to you by CodeCoffee

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Premium Theme Designed By IBTheme.com

  • Create New...