Sunday, June 13, 2010

Writing and Compiling C programs on Linux

Assume you have C source code, it needs to be compiled that means turn it in to machine language,
that your cpu can actually read.
There are two ways to do that :
1) using gcc or cc
2) using make but make requires a makefile(tells what to do).

assume you have source code file called "hello.c", once you execute following command
"gcc -o hello hello.c"

That -o option to gcc tells it what you want your program to be called and its generate a file called hello.out that would be the executable file.

//file hello.c
#include
main ()
{
printf("Hello World");
}


to compile :

$ gcc -o hello hello.c

it's generate a file called hello after that you can get output of the program using ./hello

No comments:

Post a Comment