What I usually do is make a Makefile in the src directory (which can be called from the top-level Makefile if you want), and then use these rules:
D_BIN = ../bin $(D_BIN)/%.o: %.cpp
You can also experiment only with a makefile in the top-level directory and use rules that look like this:
D_BIN = bin D_SRC = src $(D_BIN)/%.o: $(D_SRC)/%.cpp
but I didnβt use such rules, so I donβt know the pros and cons of how I usually do it. The way I usually do this works fine, I even have rules that are structured like this:
$(D_BIN)/%.d: %.cpp
and the link rule will look like this:
../dist/outexe: $(F_OBJ)
Using foreach is usually disapproving, because it does not use all the functions built into the usual makefile rules (i.e. there is no dependency on the verification based on each file, or you build everything or nothing), and as such foreach should be used only as a last resort but in this case you can make it work without using foreach.
In addition to this, there are much simpler ways to create file lists; you do not need to use a shell or sed.
F_CPP = $(wildcard *.cpp) F_OBJ = $(F_CPP:.cpp=.o)
Update: this is how I usually produce recursive make:
SUBDIRS = src .PHONY: $(SUBDIRS) all: $(SUBDIRS) $(SUBDIRS): @echo "Building $@... " $(MAKE) -C $@ $(MFLAGS)
Then, indeed, in your submaterial, you will need to use .. / bin, for example.
However, if the project is as simple as yours, you might be better off if you only have one makefile at the root level and use these rules:
D_BIN = bin D_SRC = src $(D_BIN)/%.o: $(D_SRC)/%.cpp
Recursive make files are fine (fine, but not really) if you have a really complicated directory structure, where you will add / remove / modify new tree trees as you continue. But for a simple project where you just want to have separate directories for code and objects, this is probably too large.