Suppose we have 3 files insort.c, qsort.c, shellsort.c each implementing different types of sorts and we have a main program sortdriver.c which gives the option to choose one among these different types of sort and we have a header file sort.h which has declarations for all the 3 sort functions.
(Don’t worry, these has nothing to do with Makefile J )
Essentially, we have three .c files: insort.c, qsort.c, shellsort.c
Then we have a header file sort.h which has declarations of functions used by all the three .c files. And we have the sortdriver.c which contains the main which uses these functions.
The dependencies are:
shellsort.o depends on shellsort.c and sort.h
qsort.o depends on qsort.c and sort.h
insort.o depends on insort.c and sort.h
sortdriver.o depends on sortdriver.c and sort.h
and finally, the executable sorting depends on sortdriver.o, insort.o, qsort.o and shellsort.o
Makefile is the common name for makefiles.
In Makefile, all we need to specify is these dependencies and the respective commands for compilation. Type make to compile the whole module specified by make. If some other name is used for makefile, type make –f mymakefile to us the make utility.
So type vi Makefile
In the new file, type this for creating the executable sorting.
sorting: sortdriver.o insort.o qsort.o shellsort.o
cc sortdriver.o insort.o qsort.o shellsort.o –o sorting
sortdriver.o: sortdriver.c sort.h
cc –c sortdriver.c
insort.o: insort.c sort.h
cc –c insort.c
qsort.o: qsort.c sort.h
cc –c qsort.c
shellsort.o: shellsort.c sort.h
cc –c shellsort.c
#This symbol is used for comments
# Read the makefile from bottom to top to understand the dependencies
#The space in front of commands is a tab
Now if your makefile’s name is Makefile, type make to compile your whole program whenever you make changes to any of these files. Else, type make –f mymakefile if you makefile’s name is mymakefile.
No comments:
Post a Comment