linux poison RSS
linux poison Email

How To Write, Compile and Execute C Programs under Linux

Most Linux and Unix programs are written in C. When you download source for a project, it will often be C or C++ source code. You don't necessarily need to know a darn thing about C or anything else to compile the source if you aren't changing it. It may be helpful for you to understand a bit if you are having problems with the compile, but even that isn't really necessary.

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 vi.

Source Code:
Write a Hello World C Program: Create a file call "firstprogram.c" in vi and type the following content into this file and save it.
#include<stdio.h>
main()
{
   printf("Hello World\n");
   printf("My First C Program\n");
}
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 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 firstprogram.c

Would create an executable by the name hello for your source code named firstprogram.c
Running the executable that you created is as simple as typing the following at the prompt.

$ ./hello

Or whatever you named your executable.


2 comments:

Anonymous said...

Thanx a lot for this tutorial. I am new to linux for me it was even hard to find text editor and terminal to run program.Some how I managed to find and tried this tutorial.It really worked.

Great work..!!!
:)

Anonymous said...

thanks a lot for this tutorial ....

Post a Comment

Related Posts with Thumbnails