I cannot comprehend why you would place the Makefile in its own directory; typically it would reside within the directory containing the Sources directory or within Sources directory itself.
Consider the following directory structure:
Code:
workspace/
Project1/
Makefile
Sources/
Sources1/
HelloWorld.c
Sources2/
Main.c
Objects/
Sources1/
HelloWorld.o
Sources2/
Main.o
Project2/
...
In the example above, the workspace contains two projects, each of which has a Makefile and a Source and Object directories. The Source directory contains individual directories itself for related source code files. This is not always necessary; it depends on the complexity of the project.
Anyhow, any object file(s) that are created are placed into separate directories within Objects. There is no benefit to this, other than it keeps everything organized.
I did not have too much time to play around with the puzzle that you presented, but I came up with the following Makefile that assumes something similar to the above. Actually, it assumes that there is a sole source file in Sources.
Here's the Makefile, which I'm sure could be improved...
Code:
APP = Hello
SRCDIR = Sources
OBJDIR = Objects
SRCS := $(shell find $(SRCDIR) -name '*.c')
SRCDIRS := $(shell find . -name '*.c' -exec dirname {} \; | uniq)
OBJS := $(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
CFLAGS = -Wall -pedantic -ansi
LDFLAGS =
all: $(APP)
$(APP) : buildrepo $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) -o $@
$(OBJDIR)/%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
$(RM) $(OBJS)
distclean: clean
$(RM) $(APP)
buildrepo:
@$(call make-repo)
define make-repo
for dir in $(SRCDIRS); \
do \
mkdir -p $(OBJDIR)/$$dir; \
done
endef
P.S. I've also attached a tar-ball in case you want the full work area.
Bookmarks