Makefile recompiles after cleaning objects (but not the name target)

时间:2015-06-25 10:00:18

标签: makefile

When I call the clean rule with make clean all objects are correctly deleted. However if just after I call the all rule with make it recompiles all the objects again, even if the target is already there.

As far as I know, it should not recompile the objects beacuse the $(NAME) dependecy in the all rule is already satisfied. Indeed make clean erase just the object and not the program target too.

Somebody can explain me how to avoid recompiling after a make clean call? Thank you.

Here is the makefile:

NAME = myprogram

CC = clang++
CFLAGS = -Werror -Wextra -Wall -O3 -std=c++11

DIR_SRCS = srcs/
DIR_OBJS = objs/
DIR_INCS = incs/

FILES = main.cpp \
        file1.cpp \
        files2.cpp \
        file3.cpp \

OBJS = $(addprefix $(DIR_OBJS), $(notdir $(addprefix $(DIR_SRCS), $(FILES:.cpp=.o))))

all: $(NAME)

$(NAME): $(OBJS)
    $(CC) $(OBJS) $(CFLAGS) -I$(DIR_INCS) -o $(NAME)

$(DIR_OBJS)%.o: $(DIR_SRCS)%.cpp
    mkdir -p $(DIR_OBJS)
    $(CC) $(CFLAGS) -I$(DIR_INCS) -c $< -o $@

clean:
    rm -rf $(DIR_OBJS)

fclean: clean 
    rm -f $(NAME)

re: fclean all

.PHONY: all clean fclean re

1 个答案:

答案 0 :(得分:0)

$(NAME)规则中的all依赖关系,因为$(NAME)取决于$(OBJS) - 而$(OBJS)确实如此不存在(在make clean之后)。这就是make的工作原理。如果任何依赖关系不存在或比目标更新 - 需要重新生成依赖关系(反过来 - 目标)。

例如:program - &gt; program.o - &gt; (program.c, program.h)

如果program.cprogram.hprogram.o更新,则program.o需要重新生成(更新)。这会导致program.oprogram更新 - program需要重新链接(使用program.o)。

相关问题