title: Using target-specific variable in makefile
body: <p>I have the following makefile:</p>
  
  <pre><code>OUTPUTDIR = build
  
  all: v12target v13target
  v12target: INTDIR = v12
  v12target: DoV12.avrcommontargets
  v13target: INTDIR = v13
  v13target: DoV13.avrcommontargets
  
  %.avrcommontargets: $(OUTPUTDIR)/%.elf
      @true
  
  $(OUTPUTDIR)/%.elf: $(OUTPUTDIR)/$(INTDIR)/main.o
      @echo TODO build ELF file from object file: destination $@, source $^
      @echo Compiled elf file for $(INTDIR) &gt; $@
  
  $(OUTPUTDIR)/$(INTDIR)/%.o: %.c
      @echo TODO call GCC to compile C file: destination $@, source $&lt;
      @echo Compiled object file for $&lt;, revision $(INTDIR) &gt; $@
  
  $(shell rm -rf $(OUTPUTDIR))
  $(shell mkdir -p $(OUTPUTDIR)/v12 2&gt; /dev/null) $(shell mkdir -p $(OUTPUTDIR)/v13 2&gt; /dev/null)
  
  .SECONDARY:
  </code></pre>
  
  <p>The idea is that there are several different code configurations that need to be compiled from the same source code.  The "all" target depends on v12target and v13 target, which set a number of variables for that particular build.  It also depends on an "avrcommontargets" pattern, which defines how to actually do the compiling.  avrcommontargets then depends on the ELF file, which in turn depends on object files, which are built from the C source code.</p>
  
  <p>Each compiled C file results in an object file (*.o).  Since each configuration (v12, v13, etc.) results in a different output, the C file needs to be built several times with the output placed in different subdirectories.  For example, "build/v12/main.o", "build/v13/main.o", etc.</p>
  
  <p>Sample output:</p>
  
  <pre><code>TODO call GCC to compile C file: destination build//main.o, source main.c
  TODO build ELF file from object file: destination build/DoV12.elf, source build//main.o
  TODO build ELF file from object file: destination build/DoV13.elf, source build//main.o
  </code></pre>
  
  <p>The problem is that the object file isn't going into the correct subdirectory.  For example, "build//main.o" instead of "build/v12/main.o".  That then prevents the main.o from being correctly rebuilt to generate the v13 version of main.o.</p>
  
  <p>I'm guessing the issue is that $(INTDIR) is a target specific variable, and perhaps this can't be used in the pattern targets I defined for %.elf and %.o.</p>
  
  <p>The correct output would be:</p>
  
  <pre><code>TODO call GCC to compile C file: destination build/v12/main.o, source main.c
  TODO build ELF file from object file: destination build/DoV12.elf, source build/v12/main.o
  TODO call GCC to compile C file: destination build/v13/main.o, source main.c
  TODO build ELF file from object file: destination build/DoV13.elf, source build/v13/main.o
  </code></pre>
  
  <p>What do I need to do to adjust this makefile so that it generates the correct output?</p>