title: Automatically generating makefile prerequisites (gnu-make)
body: <p>I've got a variable in one of my makefiles that contains a list of archive files I need to generate of the following form:</p>
  
  <pre><code>/libpath/mylib1/obj/mylib1.a
  /libpath/mylib2/obj/mylib2.a
  /libpath/mylib3/obj/mylib3.a
  </code></pre>
  
  <p>and so on. I've set up a rule to create each of these, basically it scans the parent folder for .cpp files and creates .o prerequisites within the /obj subfolder (so if /libpath/mylib1 contains foo.cpp and bar.cpp then the prerequisites are /libpath/mylib1/obj/foo.o and /libpath/mylib1/obj/bar.o etc). The only way I could get this to work was with .SECONDEXPANSION: I create a list of .cpp files, strip the folder and extension, suffix it with the .o extension and then prefix it with the target folder:</p>
  
  <pre><code>%.a: $$(addprefix $$(dir $$@),$$(addsuffix .o,$$(basename $$(notdir $$(wildcard $$(dir $$@)../*.cpp)))))
  </code></pre>
  
  <p>This works fine but I can't help but feel I'm going about this in an overly complicated way. Is there a better/cleaner way to do stuff like this?</p>