title: Makefile: compiling several objects with single target
body: <p>I have several c++ objects that I'd like to compile using a single target. If possible I'll make a variable in the makefile that list all the object files and then have a single target that compiles them all.</p>
  
  <p>Following <a href="https://stackoverflow.com/questions/8822591/writing-a-makefile-rule-for-a-single-target-with-multiple-dependacies">this question</a> I have the following "Makefile" so far:</p>
  
  <pre><code>#### Directories and flags
  
  ifndef $(DIR_MAIN)
    DIR_MAIN=../..
  endif
  
  DIR_EXE=$(DIR_MAIN)
  DIR_SRC=$(DIR_MAIN)/src/Analyse_MC
  DIR_MISC_SRC=$(DIR_MAIN)/src/Misc
  DIR_BLD=$(DIR_MAIN)/build/Analyse_MC
  DIR_MISC_BLD=$(DIR_MAIN)/build/Misc
  COMP=g++
  
  COMPILE_FLAGS= -std=c++11 -O3 -lstdc++ `pkg-config --cflags eigen3` -msse2 -I${DIR_MISC_SRC}
  LINK_FLAGS= -O3 -fopenmp -lgsl -lgslcblas -lm -lhdf5_cpp -lhdf5
  
  OBSERVABLE_OBJECTS=g_decomp_IP_orthog_sphere.o g_decomp_LS_orthog_sphere.o g_decomp_IP_disk.o g_decomp_LS_disk.o find_rank.o eigenvectors.o GramSchmidt.o diagonal_Hamiltonian.o mean_log_WF.o energy.o JK_EP_overlap.o overlap.o geo_mean_eigstate_overlap.o eigstate_overlap.o pair_corr_bins.o
  
  
  #### Compile all
  
  all: setup observables $(DIR_EXE)/Analyse_MC
  
  setup:
      @mkdir -p $(DIR_BLD)
  
  #### Compile observable objects
  
  observables: $(addsuffix -stamp,$(OBSERVABLE_OBJECTS))
  
  %.-stamp : %.o
      ${COMP} -c -o $@ $&lt;
  </code></pre>
  
  <p>However when running <code>make</code> I get the following:</p>
  
  <pre><code>make: *** No rule to make target `g_decomp_IP_orthog_sphere.o-stamp', needed by `observables'.  Stop.
  </code></pre>
  
  <p>So I've misunderstood something.</p>
  
  <p>If possible I'd also like each of the objects to be updated with changes in <code>.h</code>-files with the name of the object in addition to some common <code>.h</code>-files for all of the objects. Is this possible?</p>
  
  <p>Or do I have to / is it recommended to write a separate target for each object?</p>
  
  <p>EDIT:</p>
  
  <p>Some info about the variables:</p>
  
  <p>DIR_MAIN is defined the way it is because usually this makefile will be called from another makefile which defines DIR_MAIN from its directory using <code>pwd</code>; but ut can also be called on its own from its own directory.</p>
  
  <p>DIR_MISC_SRC points to some header files necessary for the files in DIR_SRC.</p>
  
  <p>DIR_BLD and DIR_MISC_BLD will contain the corresponding resulting object files.</p>