如何避免我的makefile重新链接 [英] How to avoid my makefile to relink
问题描述
我的Makefile正在重新链接,我找不到原因.
My Makefile is relinking and I can't find why.
我不确定为什么malloc假定必须执行$(NAME). $(SRC:.c = .o)宏会更改.o文件的时间戳还是类似的内容?
I'm not sure why malloc assume that $(NAME) have to be executed. Is the $(SRC:.c=.o) macro changing the timestamps of the .o files or something like that ?
CC = gcc
NAME = app
#
CFLAGS = -Wall -Werror -Wextra -pedantic -pedantic-errors
INCLUDES = -I ./includes
#
DIRSRC = srcs/
DIROBJ = objs/
SRC += main.c
SRC += malloc.c
OBJ = $(SRC:.c=.o)
DIROBJS = $(addprefix $(DIROBJ), $(OBJ))
#
LIBS_PATH = ./libs
LIBFT_PATH = $(LIBS_PATH)/libft
LIBFT_INCLUDES = -I $(LIBFT_PATH)
LIBFT = -L $(LIBFT_PATH) -lft
#
COMPILE = $(CC) $(CFLAGS) $(INCLUDES)
#
all: $(NAME)
$(NAME): configure libs $(DIROBJS)
$(COMPILE) $(LIBFT) $(DIROBJS) -o $(NAME)
$(DIROBJ)%.o: $(DIRSRC)%.c
@echo Compiling: $<
$(COMPILE) $(LIBS_INCLUDES) -c $< -o $@
clean:
@rm -rf $(DIROBJ)
fclean: clean
@rm -rf $(NAME)
re: fclean all
#
configure:
@mkdir -p $(DIROBJ)
#
libs:
@$(MAKE) -C $(LIBS)
.PHONY: all configure clean fclean re libs cleanlibs fcleanlibs relibs
推荐答案
@rtur的答案有效,但是我应该提到另一种方法.您可以这样做:
@rtur's answer works, however I should mention another alternative. You could do:
$(DIROBJ):
mkdir $@
$(DIROBJ)/%.o: $(DIRSRC)/%.c | $(DIROBJ)
@echo Compiling: $<
$(COMPILE) $(LIBS_INCLUDES) -c $< -o $@
这样,仅当目录不存在时才创建目录.要注意的一件事是|
符号.这使$(DIROBJ)
成为仅订购的先决条件.这意味着,如果它比目标新,则不会导致目标重建.这对于目录确实非常重要,因为目录的时间戳记是目录中最后一项的添加/删除/修改的日期,并且如果没有该符号,您的目标将始终过期.这被认为是更干净的,因为这样您对mkdir
的调用较少.
That way, it only makes the directory if it doesn't already exist. One thing to notice is the |
symbol. This makes $(DIROBJ)
an order-only prerequisite. This means if it's newer than the target, it will not cause the the target to rebuild. This is really important for directories, as the timestamp of a directory is the date the last item in it was added/deleted/modified, and your target would always be out of date without that symbol. This is considered cleaner, as you have less invocations of mkdir
this way.
此外,作为样式说明,通常,您不会在目录名称的末尾包括结尾的/
. $(OBJ_DIR)/%.o
看起来比$(OBJ_DIR)%.o
好.当然,那可能只是我的意见:-)
Also, as a style note, you usually, you don't include the trailing /
at the end of directory names. $(OBJ_DIR)/%.o
looks nicer than $(OBJ_DIR)%.o
. Of course, that could just be my opinion :-)
这篇关于如何避免我的makefile重新链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!