Thursday, November 4, 2010

My first Makefile

A starter’s guide to a makefile
This post is just the tip of the iceberg called “make” and its capabilities. It does not even scratch the surface but is meant as a tutorial to write your first makefile.
The following Makefile is used to make it easier to type commands to compile and run a program.
for eg. to compile arg.c
we use
gcc -o arg arg.c
Instead of this we can create a Makefile as:
#Makefile
1:
(tab) gcc -o arg arg.c
and save it as Makefile in the directory where our program arg.c is saved.
Now we can type:
$ make 1
to compile the program
and
./arg
to run the program.
anand@anand-desktop:~/Desktop$ make 1
Makefile:5: *** missing separator. Stop.
This error comes if we have missed the tab in the newline before gcc -o arg arg.c
1:
(Tab) gcc -o arg arg.c
Make sure you press the tab key and not write (Tab) .
If we can compile a program, why not run it too?
edit the Makefile and add the following:
2:
(tab) ./arg
Therefore,
$make 1
$make 2
in your terminal is same as
gcc -o arg arg.c
followed by
./arg

http://oddabout.com/?p=78

No comments:

Post a Comment