0

I was trying to adjust my standard makefile to include vpath. I got it working as I want, however it keeps relinking now. Anybody knows what I'm doing wrong? Does it have to do with my usage of vpath?

Relevant part of my makefile:

NAME        :=  minishell

# Directories
INCL_DIR    :=  includes
SRCS_DIR    :=  srcs
OBJDIR      :=  objs
vpath       %.c $(SRCS_DIR)
vpath       %.c $(SRCS_DIR)/executor

# Config
CC          :=  gcc
FLAGS       :=  -Wall -Wextra

# Srcs
SRCS        :=  main.c \
                get_cmd.c \
                exit_error.c \
                ft_strlen.c
OBJS        :=  $(SRCS:.c=.o)


all:        $(NAME)

$(NAME):    $(OBJS)
    $(CC) $(addprefix $(OBJDIR)/, $(OBJS)) $(FLAGS) -o $(NAME)

%.o: %.c
    @mkdir -p $(OBJDIR)
    $(CC) $(FLAGS) -c $< -o $(addprefix $(OBJDIR)/, $@) -I$(INCL_DIR)

Project folder structure (simplified):

Minishell/
    includes/
    srcs/
        executor/
        main.c
    Makefile

Thanks a lot!

  • 3
    It is always wrong for any make recipe to build a file that is not exactly equal to the value of `$@`. Here your `%.o` recipe builds `$(addprefix $(OBJDIR)/, $@)` which is not the same as `$@`, so your makefile is wrong. You have told make that you would build a file `main.o`, but you didn't, you built a file `obj/main.o`. Now the next time make runs, it says "is there a file `main.o` that exists and is up to date?" And there is no such file, so make will re-build it. – MadScientist Dec 20 '21 at 15:18
  • The usual objective of enabling `VPATH` is to provide for out-of-source builds, and the purpose of out-of source builds is to keep the source directory clean (for any of a variety of underlying reasons). The objective, if there actually is one, of building targets into special-purpose directories instead of in the default locations is almost always one of the clean-directory objectives that is also served by `VPATH`. Consider, then, choosing *one*. – John Bollinger Dec 20 '21 at 16:22
  • Another way VPATH can be used is for in-source builds where the source files live in multiple directories but the object files live in the same directory and you don't want to write lots of pattern rules. That seems to be the objective in this question. Note, it's never a workable use of VPATH to find object files (and these vpath entries are clearly restricted to source files so that's good). – MadScientist Dec 20 '21 at 17:21

0 Answers0