Makefile每次都会建立目标 [英] Makefile builds target every time
问题描述
我编写了一个makefile来编译不同目录中的所有源并构建目标.即使文件没有更改,我运行make时,它也不会重新编译未修改的文件.但是,它始终可以建立目标.为什么我每次运行make时都单独建立目标?
I have written a makefile to compile all the sources in different directories and build a target. When I run make even when there is no change in the files, it does not re-compiles the unmodified files. But, it always builds the target. Why is the target alone getting built every time when I run make?
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))
每次都从目标文件中构建$(TARGET).请指导我我要去哪里了.
The $(TARGET) gets built from the object files each time. Please guide me where I'm going wrong.
更新:
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))
这每次都会构建所有文件.
This builds all the files every time.
最终解决方案:
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): $(OUTDIRS) $(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!"
$(OUTDIRS):
@mkdir -p $@
-include $(wildcard $(OBJS:.o=.d))
最终解决方案按预期工作!
The final solution works as expected!
推荐答案
您可以使用make的调试输出来查找其认为需要重建的内容:
You can use the debug output of make to look for what it thinks needs rebuilding:
make -d | grep remake
您可能会看到类似这样的内容:
You'll probably see something like this:
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)
的依赖项,因此它也会重新构建后者.
Which shows that it always thinks that the builddir
target needs to be remade. Since that is a dependency of $(TARGET)
it rebuilds the latter as well.
如果使用规则而非功能创建规则,则make
将知道是否需要创建它们.例如,添加变量$(OUTDIRS)
(它的规则),并将其作为编译的依赖项:
If you use a rule to create your build directories instead of a function, make
will know whether it needs create them or not. For example, adding the variable $(OUTDIRS)
, a rule for it, and making this a dependency of compiling:
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))
这篇关于Makefile每次都会建立目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!