Makefile5解决了头文件中添加头文件的时候直接修改后make all无效的问题
1)Hello.h里面引用了other.h修改other.h没效果
为.dep文件生成依赖关系,在.o与.c文件的依赖关系生成好了要修改目录的时候顺便把.dep文件也加进去
2)make clean两次发现结果不同,第二次居然重构了目标集合然后删除
为重复的部分加入条件执行,比如是include 部分重复了,则为include加入条件
- .PHONY:all clean
-
-
CC = gcc
-
RM = rm
-
RMCMD = -rf
-
MKDIR = mkdir
-
-
DIR_OBJS = objs
-
DIR_EXES = exes
-
DIR_DEPS = deps
-
DIR = $(DIR_OBJS) $(DIR_EXES) $(DIR_DEPS)
-
SRCS = $(wildcard *.c)
-
OBJS = $(SRCS:.c=.o)
-
OBJS := $(addprefix $(DIR_OBJS)/,$(OBJS))
-
EXE = complicated
-
EXE := $(addprefix $(DIR_EXES)/,$(EXE))
-
DEPS = $(SRCS:.c=.dep)
-
DEPS := $(addprefix $(DIR_DEPS)/,$(DEPS))
-
-
ifeq ("$(wildcard $(DIR_OBJS))", "")
-
DEP_DIR_OBJS := $(DIR_OBJS)
-
endif
-
-
ifeq ("$(wildcard $(DIR_EXES))", "")
-
DEP_DIR_EXES := $(DIR_EXES)
-
endif
-
-
ifeq ("$(wildcard $(DIR_DEPS))", "")
-
DEP_DIR_DEPS := $(DIR_DEPS)
-
endif
-
-
all:$(EXE)
-
-
$(DIR):
-
$(MKDIR) $@
-
-
#1)sed中无$@的情况:生成%.o文件的依赖关系;
-
#2)sed中有$@的情况:生成%.o和%.dep文件的依赖关系;
-
$(DIR_DEPS)/%.dep:$(DEP_DIR_DEPS) %.c
-
@echo "Creating $@ ..."
-
@set -e; \
-
$(RM) $(RMCMD) $@.tmp; \
-
$(CC) -E -MM $^ > $@.tmp; \
-
sed 's,\(.*\)\.o[ :]*,objs/\1.o $@: ,g' < $@.tmp > $@ ; \
-
$(RM) $(RMCMD) $@.tmp
-
-
#连续两次make clean得到的结果是一次仅仅做了删除的事,另一次却创建了$(DEPS)文件,故条件执行include
-
#仅在当前构建的目标名不是clean时才include
-
ifneq ($(MAKECMDGOALS),clean)
-
include $(DEPS)
-
endif
-
-
$(EXE):$(DEP_DIR_EXES) $(OBJS)
-
$(CC) -o $@ $(filter %.o,$^)
-
$(DIR_OBJS)/%.o:$(DEP_DIR_OBJS) %.c
-
$(CC) -o $@ -c $(filter %.c,$^)
-
-
clean:
-
$(RM) $(RMCMD) $(DIR)
Makefile5
阅读(1755) | 评论(0) | 转发(0) |