.PHONY:all clean
MKDIR=mkdir
RM=rm
RMFLAGS=-rf
CC=gcc
DIR_OBJS=objs
DIR_EXES=exes
DIR_DEPS=deps
DIRS=$(DIR_OBJS) $(DIR_EXES) $(DIR_DEPS)
EXE=complicated.exe
EXE:=$(addprefix $(DIR_EXES)/,$(EXE))
SRCS=$(wildcard *.c)
OBJS=$(SRCS:.c=.o)
OBJS:=$(addprefix $(DIR_OBJS)/,$(OBJS))
DEPS=$(SRCS:.c=.dep)
DEPS:=$(addprefix $(DIR_DEPS)/,$(DEPS))
ifeq ("$(wildcard $(DIR_EXES))","")
DEP_DIR_EXES := $(DIR_EXES)
endif
ifeq ("$(wildcard $(DIR_OBJS))", "")
DEP_DIR_OBJS := $(DIR_OBJS)
endif
ifeq ("$(wildcard $(DIR_DEPS))", "")
DEP_DIR_DEPS := $(DIR_DEPS)
endif
all:$(EXE)
-include $(DEPS)
$(DIRS):
$(MKDIR) $@
$(EXE):$(DEP_DIR_EXES) $(OBJS)
$(CC) -o $@ $(filter %.o,$^)
$(DIR_OBJS)/%.o:$(DEP_DIR_OBJS) %.c
$(CC) -o $@ -c $(filter %.c,$^)
$(DIR_DEPS)/%.dep:$(DEP_DIR_DEPS) %.c
@echo "Making $@..."
@set -e; \
$(RM) $(RMFLAGS) $@; \
$(CC) -E -MM $(filter %.c,$^) > $@.tmp; \
sed 's,\(.*\)\.o[:]*,objs/\1.o:,g' < $@.tmp > $@; \
$(RM) $(RMFLAGS) $@.tmp
clean:
$(RM) $(RMFLAGS) $(DIRS)
注意后面的$(RM) $(RMFLAGS) $@; \
有的会写成$(RM) $(RMFLAGS) $@.tmp; \
其实根据上下文,应该是要删除之前已经存在的dep文件。
$@.tmp这个临时文件在下面已经被删除了。