Wednesday, May 14, 2008

Make "make" work for you

Make - tool to control the compilation and creation of executables. I was hacking into this tool for quite sometime. I feel "make" is good for all the linux/unix c/c++ programmers.

Here is a simple Tutorial of Make:

source file (helloworld.c)
========
#include ...
int main() {
using namespace std;

cout<<"hello world\n"; return 0;
}
simple makefile(gnumakefile/makefile)
===========
helloworld: helloworld.c
TAB g++ -o helloworld helloworld.c

TAB - is important, otherwise gnumake will prompt for error.

command
=======
make TARGET

make helloworld

Example 2
========

/* sum.c */

int sum(int a, int b) {
return a+b;
}

/* diff.c */

int diff(int a, int b) {
return a-b;
}

/* main.c */

int main() {
...
c=sum(20,30);
...
d=diff(30,23);
...
}

/* Make file */

CC = g++ /* which compiler*/
CFLAGS = -Wall -g -ansi /* compiler flags */

sum.o: sum.c
$(CC) $(CFLAGS) -c -o $@ $<
diff.o: diff.c
$(CC) $(CFLAGS) -c -o $@ $<
main: sum.o diff.o main.c
$(CC) $(CFLAGS) -o $@ $^
clean:
-rm core *.o main
Special Tags
=========
$@ -- current target name
$LT -- First name of the prerequisite
$^ -- List of all prerequisite
$* -- List of all prerequisite (without prefix)

So, dudes, start using make. It is cool, it makes life a lot easier for c/c++ programmers.

No comments: