Jump to content

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


Recommended Posts

Posted

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

OR

$ ./hello

 

Or whatever you named your executable.

 

This is the absolute basics of compiling C programs under Linux.

 

Tip brought to you by CodeCoffee

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...