Makefile

 1 # ----------------------------------------------------------------
 2 # This is a makefile which automatically updates itself, 
 3 # using "$(CC) -MM -E" in linux.
 4 # linliu 2011-04-25
 5 # ----------------------------------------------------------------
 6 # $* 不包括扩展名的目标文件名称
 7 # $+ 所有依赖文件,以空格分开,有顺序,有重复
 8 # $< 第一个依赖文件名称
 9 # $? 所有依赖文件,修改日期比目标文件晚
10 # $@ 目标的完整名称
11 # $^ 所有不重复依赖文件,以空格分开
12 # $% 如果目标是归档成员,则该变量表示目标的归档成员名称。如目标为
13 #    mytarget.so(image.o),则 $@ 为 mytarget.so, $% 为 image.o
14 # ----------------------------------------------------------------
15 ###### common commands
16 CC = gcc
17 CFLAGS = -Wall -O2
18 CXX = g++
19 CXXFLAGS = -Wall -O2
20 RM = rm
21 ###### targets and objects
22 OBJS = $(patsubst %.cpp, %.o, $(shell ls *.cpp))
23 EXEC = ksp
24 ###### compiler rules
25 .PHONY: all clean
26 
27 all: $(EXEC)
28 
29 ksp: $(OBJS)
30     $(CXX) $(CXXFLAGS) -o $@ $+
31 ###### object file dependences
32 DEPS = $(patsubst %.o, %.dep, $(OBJS))
33 
34 -include $(DEPS)
35 
36 %.dep: %.cpp
37     @rm -f $@; \
38     $(CXX) $(CXXFLAGS) -MM -E $< > $@.$$$$; \
39     sed 's,\($*\)\.o[ :]*,\1.o $@: ,g' < $@.$$$$ > $@; \
40     rm -f $@.$$$$
41 
42 clean:
43     -rm -f $(EXEC) $(OBJS) $(DEPS)

posted on 2012-09-14 10:35  究生  阅读(210)  评论(0)    收藏  举报

导航