Makefile to put object files from source files different directories into a single, separate directory?

There’s more than one way to do it, but this is a pretty good one (I really should have that hotkeyed).

vpath %.cpp ../src

src = Foo.cpp Bar.cpp 
test_src = Main.cpp FooTest.cpp BarTest.cpp 

objects = $(patsubst %.cpp,obj/%.o,$(src)) 
test_objects = $(patsubst %.cpp,obj/%.o,$(test_src)) 

$(objects): | obj

obj:
  @mkdir -p $@

obj/%.o : %.cpp
  @echo $< 
  @$(CXX) $(CXXFLAGS) -c $< -o $@

Leave a Comment