My question is an extension of the question Can I compile all .cpp files in src/ to .o's in obj/, then link to binary in ./?
My project has the similar structure, but I have far more then one src folder, including folders, that are outside of my root /project dir:
/somedir
  /project
    Makefile
    main
    /src1
      main.cpp
      foo.cpp
      foo.h
    /src2
      bar.cpp
      bar.h
    ...
    /srcn
      baz.cpp
      baz.cpp
    /obj
      main.o
      foo.o
      bar.o
      ...
      baz.o
      alpha.o
      betta.o
      ...
      zetta.o
  /ext_src1
    alpha.cpp
  /ext_src2
    betta.cpp
  ...
  /ext_srcn
    zetta.cpp
In my Makefile I have list of all cpp's I need, collected partly manually, partly with the help of wildcard. I assume, that the names of all cpp files are different (and their objects can safely be put into one dir). Now, how can I compile all them, into my obj folder?
I managed to do almost what I want in the following way:
SOURCES=${wildcard src1/*.cpp} 
SOURCES+=${wildcard src2/*.cpp} 
...
SOURCES+=../ext_srcn/zetta.cpp 
OBJECTS=$(SOURCES:.cpp=.o)
main: $(OBJECTS)
    $(CC) $(LD_FLAGS) -o $@ $^
.cpp.o:
    $(CC) $(CFLAGS) -c $< $(INCLUDES) -o $(addprefix obj/,$(notdir $@))
But now, of course, all my sources are recompiled every time, even when no changes are made. That is terrible. How can I trace the origin of my .o target on obj ? Or may be there is another way to solve my task ?
 
     
    