linux poison RSS
linux poison Email

How to compile & execute C programs under Linux

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.


1 comments:

Anonymous said...

I think maybe you can add a couple of more flags for beginners :
-Wall and -g
-Wall is for turning on all possible warnings. This is a good practise, especially for beginners, so that they understand where the code can be improved at an early stage.
-g is for adding debugging information. This is useful when debugging the program using a debugger like gdb

Post a Comment

Related Posts with Thumbnails