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
答案 0 :(得分:0)
$(NAME)
规则中的all
依赖关系不,因为$(NAME)
取决于$(OBJS)
- 而$(OBJS)
确实如此不存在(在make clean
之后)。这就是make
的工作原理。如果任何依赖关系不存在或比目标更新 - 需要重新生成依赖关系(反过来 - 目标)。
例如:program
- &gt; program.o
- &gt; (program.c, program.h)
如果program.c
或program.h
比program.o
更新,则program.o
需要重新生成(更新)。这会导致program.o
比program
更新 - program
需要重新链接(使用program.o
)。