0

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?

Joe Carter
  • 43
  • 2
  • 4
  • 1
    Makefile for building source tree into separate build directory with preserved source tree structure - http://stackoverflow.com/a/41924169/4224163 – Alexey Semenyuk Feb 04 '17 at 20:43

1 Answers1

3

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

Michael Livshin
  • 401
  • 3
  • 4