Noel
Noel

Reputation: 145

Passing target name in dependencies which can be formatted in a makefile

What I would like to do is to get the target name in the dependency and be able to format it. For example if the target is Objs/foo.o I would like to make the dependency to Sources1/foo.c, I tried doing this

$(filter %.o,$(OBJS)): $(filter %/$(patsubst %.o,%,$(notdir $@)).cpp, $(SRCS))

but none of them works. Where OBJS is a list of object files ex. Objs/foo.o Objs/moo.o etc.. Any help would be greatly appreciated. And SRCS is a list of different directories were sources can be located. Example Sources1/ and Sources2/ where I need to search in both those directories to find where the source is.

Upvotes: 1

Views: 132

Answers (1)

Louis
Louis

Reputation: 151511

Using the vpath directive could take care of your issue. Here's an example:

OBJS:=Objs/foo.o Objs/bar.o

all: $(OBJS)

# This tells make to search for %.c files in Sources1 and Sources2.
vpath %.c Sources1 Sources2

$(filter %.o,$(OBJS)): Objs/%.o : %.c
    @echo $@ $^

When I run it here with foo.c in Sources1 and bar.c in Sources2, I get the following output:

Objs/foo.o Sources1/foo.c
Objs/bar.o Sources2/bar.c

Upvotes: 1

Related Questions