Joe Carter
Joe Carter

Reputation: 43

Using functions with automatic variables in makefiles

I am trying to write a make file that compiles all of my C files under the source directory into the same file structure in /build/obj

For example one of my c files might be /source/foo/bar.c

And I want that to compile and be put in /build/obj/foo/bar.o

Here is what I've got so far while I was experimenting:

FILES = $(shell find source/ -type f -name '*.c')
OBJS = $(patsubst $(SOURCE)/%.c,$(OBJ)/%.o, $(FILES))

all: build/obj/foo/bar.o
    echo "DONE"

%.o:
    echo $(patsubst build/obj, source, $@)

However the patsubst function doesn't seem to be working with the automatic variable $@, instead I get "build/obj/foo/bar.o".

Is there a reason why automatic variables don't work with functions in make? If so how do I fix it?

Upvotes: 0

Views: 498

Answers (1)

Michael Livshin
Michael Livshin

Reputation: 401

patsubst does nothing without a pattern, try $(patsubst build/obj/%,source/%,$@)

Upvotes: 3

Related Questions