consider a program prog that consists of 3 source files
The following figure shows the compilation process:
TargetName: depend-file1 depend-file2 .... <TAB> Command 1 (to build "TargetName") <TAB> Command 2 (to build "TargetName") <TAB> Long command 2 to \ <TAB> build "TargetName" ... <TAB> Command N to build "TargetName" |
prog: f1.o f2.o f3.o <TAB> CC -o prog f1.o f2.o f3.o f1.o: f1.C <TAB> CC -c f1.C f2.o: f2.C <TAB> CC -c f2.C f3.o: f3.C <TAB> CC -c f3.C |
File prog depends on the files f1.o f2.o f3.o
(Makefile is preferred, because the ls command will list the file early)
(The MAKE utility does this by checking the time stamps on the source files, the object files, and the target files to determine which source files need to be re-compiled....)
UNIX>> make |
Save all the files in the same directory and type:
After compiling the file, it will link the other object files (that do NOT need re-compiling) into the program rect-rule4
ALL: TargetName1 TargetName2 .... <EMPTY LINE !!!> TargetName1: dependency-list-1 .... TargetName2: dependency-list-2 .... |
Example:
UNIX>> make TargetName1 |
will make the target FONT color="red"> TargetName1
But it is a whole lot EASIER if you DO...
Example:
make -f Any-FileName-as-your-Makefile |
|
Definition of a macro: MACRO_NAME = any string |
This expression $(MACRO_NAME) will be replaced by the string in the MACRO_NAME definition
OBJECTS = f1.o f2.o f3.o COMPILER = CC -g prog: $(OBJECTS) <TAB> $(COMPILER) -o prog $(OBJECTS) f1.o: f1.C <TAB> $(COMPILER) -c f1.C f2.o: f2.C <TAB> $(COMPILER) -c f2.C f3.o: f3.C <TAB> $(COMPILER) -c f3.C |
OBJECTS = f1.o f2.o f3.o COMPILER = CC -fast prog: $(OBJECTS) <TAB> $(COMPILER) -o prog $(OBJECTS) f1.o: f1.C <TAB> $(COMPILER) -c f1.C f2.o: f2.C <TAB> $(COMPILER) -c f2.C f3.o: f3.C <TAB> $(COMPILER) -c f3.C |
OBJECTS = f1.o f2.o f3.o COMPILER = g++ -g prog: $(OBJECTS) <TAB> $(COMPILER) -o prog $(OBJECTS) f1.o: f1.C <TAB> $(COMPILER) -c f1.C f2.o: f2.C <TAB> $(COMPILER) -c f2.C f3.o: f3.C <TAB> $(COMPILER) -c f3.C |
Example usage:
prog: file1.o file2.o file3.o <TAB> CC -o $@ file1.o file2.o file3.o |
Example usage:
file1.o: file1.C <TAB> CC -c $< |
file1.o: file1.C CC -c file1.C file2.o: file2.C CC -c file2.C ... |
# Tells MAKE that you will be using these special suffixes # to make your own rules. .SUFFIXES: .C .o <-- This define the special suffixes .C.o: CC -c $< |
This rules says that when ever you see a file FILENAME.o, it will depend on the file FILENAME.C.
The way make the file FILENAME.o is: CC -c FILENAME.C
.C.o: CC -c $< libMyLib.a: file1.o file2.o file3.o ar -rv $@ file1.o file2.o file3.o |