I'm trying to write a portable BSD and GNU Makefile that does an out-of-source build and automatic prerequisites. GNU Make works fine but BSD Make does not.
OBJDIR := objdir
SRCS := foo.c bar.c baz.c
OBJS := $(SRCS:%.c=$(OBJDIR)/%.o)
CPPFLAGS += -MMD -MP
all: $(OBJDIR) $(OBJS)
$(CC) $(LDFLAGS) -o main $(OBJS) $(LDLIBS)
$(OBJDIR)/%.o: %.c
$(COMPILE.c) $(OUTPUT_OPTION) $<
$(OBJDIR):
mkdir $(OBJDIR)
-include $(OBJDIR)/$(SRCS:%.c=%.d)
clean:
rm -rf main $(OBJDIR)
This is the output:
$ gmake
mkdir objdir
cc -MMD -MP -c -o objdir/foo.o foo.c
cc -MMD -MP -c -o objdir/bar.o bar.c
cc -MMD -MP -c -o objdir/baz.o baz.c
cc -o main objdir/foo.o objdir/bar.o objdir/baz.o
$ bmake
--- objdir ---
bmake: bmake: don't know how to make objdir/foo.o. Stop
bmake: stopped in /home/antonin/tests
My guess is that BSD Make does not understand substitution within a path (is that the right term?) $(OBJDIR)/%.o: %.c
, because bmake OBJDIR=.
works fine. If so, how can I write a simple, portable Makefile that achieves the same functionality?
Thank you!