我编写了一个makefile来编译不同目录中的所有源代码并构建目标。当我运行make时,即使文件没有变化,它也不会重新编译未修改的文件。但是,它总是建立目标。为什么每次运行make时都会单独构建目标?
// 'app'=>'required|mimes:application/vnd.android.package-archive'
$(TARGET)每次都是从目标文件构建的。请指导我出错的地方。
更新
CC = gcc
CFLAGS = $(INCLUDES)
TARGET = FinalBin
OUTDIR := obj
INCLUDES = -Isrc/in
S1 = src/s1
S2 = src/s2
S1_SRC = $(wildcard $(S1)/*.c)
S2_SRC = $(wildcard $(S2)/*.c)
SRCS := $(S1_SRC) \
$(S2_SRC)
OBJS := $(patsubst %.c,$(OUTDIR)/%.o,$(SRCS))
SRCDIRS := $(S1) $(S2)
all: $(TARGET)
$(TARGET): builddir $(OBJS)
@echo "Building..." $@
@$(CC) $(OBJS) $(CFLAGS) -o $@
@echo "Build Complete!"
$(OUTDIR)/%.o: %.c
@echo "Compiling.." $(notdir $<)
@$(CC) $(CFLAGS) -MMD -c $< -o $@
clean:
-@rm -rf $(OUTDIR) FinalBin
@echo "Clean complete!"
builddir :
@$(call create-dir)
define create-dir
for dir in $(SRCDIRS); \
do \
mkdir -p $(OUTDIR)/$$dir; \
done
endef
-include $(wildcard $(OBJS:.o=.d))
每次都会构建所有文件。
最终解决方案:
CC = gcc
CFLAGS = $(INCLUDES)
TARGET = FinalBin
OUTDIR := obj
INCLUDES = -Isrc/in
S1 = src/s1
S2 = src/s2
S1_SRC = $(wildcard $(S1)/*.c)
S2_SRC = $(wildcard $(S2)/*.c)
SRCS := $(S1_SRC) \
$(S2_SRC)
OBJS := $(patsubst %.c,$(OUTDIR)/%.o,$(SRCS))
SRCDIRS := $(S1) $(S2)
OUTDIRS := $(addprefix $(OUTDIR)/,$(SRCDIRS))
all: $(TARGET)
$(TARGET): $(OBJS)
@echo "Building..." $@
@$(CC) $(OBJS) $(CFLAGS) -o $@
@echo "Build Complete!"
$(OUTDIR)/%.o: $(OUTDIRS) %.c
@echo "Compiling.." $(notdir $(filter %.c,$^))
@$(CC) $(CFLAGS) -MMD -c $(filter %.c,$^) -o $@
clean:
-@rm -rf $(OUTDIR) FinalBin
@echo "Clean complete!"
$(OUTDIRS):
mkdir -p $@
builddir :
@$(call create-dir)
define create-dir
for dir in $(SRCDIRS); \
do \
mkdir -p $(OUTDIR)/$$dir; \
done
endef
-include $(wildcard $(OBJS:.o=.d))
最终解决方案按预期工作!
答案 0 :(得分:3)
您可以使用make的调试输出来查找它认为需要重建的内容:
make -d | grep remake
您可能会看到类似这样的内容:
No need to remake target `Makefile'.
Must remake target `builddir'.
No need to remake target `src/s1/x.c'.
No need to remake target `obj/src/s1/x.o'.
Must remake target `FinalBin'.
Must remake target `all'.
这表明它始终认为builddir
目标需要重新制作。由于这是$(TARGET)
的依赖关系,因此它也会重建后者。
如果使用规则来创建构建目录而不是函数,make
将知道是否需要创建它们。例如,添加变量$(OUTDIRS)
,它的规则,并使其成为编译的依赖:
SRCDIRS := $(S1) $(S2)
OUTDIRS := $(addprefix $(OUTDIR)/, $(SRCDIRS)) # <---------- Add this variable
All: $(TARGET)
$(TARGET): $(OBJS)
@echo "Building..." $@
@$(CC) $(OBJS) $(CFLAGS) -o $@
@echo "Build Complete!"
$(OUTDIR)/%.o: $(OUTDIRS) %.c # <--------------------------- Add dependency to $(OUTDIRS)
@echo "Compiling.." $(notdir $<)
@$(CC) $(CFLAGS) -MMD -c $< -o $@
clean:
-@rm -rf $(OUTDIR) FinalBin
@echo "Clean complete!"
$(OUTDIRS): # <--------------------------------------------- Add rule
mkdir -p $@
-include $(wildcard $(OBJS:.o=.d))