0

I am doing a school project, my makefile is always relinking and I can't figure out why. I need to compile the objs with a library (libft that will have a libft.a file, so I need to create the library using other makefile). Also what are the best resources to learn makefile?

SRCS = src/pipex.c \
       src/utils.c \

OBJS = $(SRCS:.c=.o)

NAME = pipex
LIBFT       = libft.a
LIBFT_DIR := ./libft
LIBFT_LIB := $(LIBFT_DIR)/$(LIBFT)
CC = cc
FLAGS = -Wall -Wextra -Werror
LIBC = ar rc
RM = rm -f

all: $(NAME)

$(NAME): $(OBJS) $(LIBFT)
    $(CC) $(FLAGS) $(OBJS) $(LIBFT_LIB) -o $(NAME)

debug: $(OBJS) $(LIBFT)
    $(CC) $(FLAGS) -g $(SRCS) $(LIBFT_LIB) -o $(NAME)
    
$(LIBFT):
    @cd $(LIBFT_DIR) && $(MAKE) --silent

clean:
    cd $(LIBFT_DIR) && $(MAKE) clean
    $(RM) $(OBJS) $(BNS_OBJS)

fclean: clean
    cd $(LIBFT_DIR) && $(MAKE) fclean
    $(RM) $(NAME)

re: fclean all

.PHONY: $(LIBFT)

I have tried to change the libft command to silent so no output to the screen and change the OBJS to the SRCS in $(NAME) command.

snowhp
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. Are you sure you want the standard and debug versions of the executable to have the same name? – Beta Apr 08 '23 at 23:23
  • Could happen if `libft.a` timestamp changes every time. – Alex Cohn Apr 09 '23 at 06:33
  • @Beta Yes, there is no problem if they have the same name, is just for me to test. – snowhp Apr 09 '23 at 09:12
  • @AlexCohn I was testing and removed the execution of $(LIBFT) in $(NAME) and executed with libft.a, and no "relink" (since the timestamp changed its considered a relink?) – snowhp Apr 09 '23 at 09:15
  • Yes, change of timestamp of `libft.a` file is designed to cause relink. This can be fixed within your `libft/Makefile`. – Alex Cohn Apr 09 '23 at 10:02

1 Answers1

0

The problem is a confusion of libft.a with ./libft/libft.a.

Look at this rule:

$(LIBFT):
    @cd $(LIBFT_DIR) && $(MAKE) --silent

The target is libft.a, but it doesn't actually build libft.a, it builds ./libft/libft.a. Nothing builds libft.a (in the working directory), but the pipex and debug rules have it as a prerequisite. So every time you tell Make to build one of those targets, Make sees that libft.a does not exist, so it tries to build it and then relink everything.

The fix is simple: give things accurate names:

$(NAME): $(OBJS) $(LIBFT_LIB)
    $(CC) $(FLAGS) $(OBJS) $(LIBFT_LIB) -o $(NAME)

debug: $(OBJS) $(LIBFT_LIB)
    $(CC) $(FLAGS) -g $(SRCS) $(LIBFT_LIB) -o $(NAME)
Beta
  • 96,650
  • 16
  • 149
  • 150