title: Makefile generic target rule
body: <p>I'm trying to improve the naive solution (single rules for compiling targets and additional one for linking all the previous targets together) which I used im my makefile.
  I came up with the following Makefile (clipped):</p>
  
  <pre><code>.PHONY: clean
  
  BASE_DIR = ../
  
  CC = gcc
  CROSS_COMPILE = arm-none-eabi-
  CFLAGS =-g -I${BASE_DIR}/include 
  LDFLAGS =-L${BASE_DIR}/lib -Ttarget.ld -nostdlib 
  
  SOURCES = first.c second.c third.c fourth.c
  OBJECTS = $(SOURCES:.c=.o)
  EXECUTABLE = $(OBJECTS:.o=)
  
  all: $(EXECUTABLE)
  
  %:
      ${CROSS_COMPILE}$(CC) $(CFLAGS) $(LDFLAGS) $*.c -o $*
  
  clean:
      rm ${EXECUTABLE}
  </code></pre>
  
  <p>This works fine but I'd like to separate the compilation and linking processes. Therefore I tried to modify it as follows:</p>
  
  <pre><code>.PHONY: clean
  
  BASE_DIR = ../
  
  CC = gcc
  CROSS_COMPILE = arm-none-eabi-
  
  CFLAGS =-c -g -I${BASE_DIR}/include 
  LDFLAGS =-L${BASE_DIR}/lib -Ttarget.ld -nostdlib 
  
  SOURCES = first.c second.c third.c fourth.c
  OBJECTS = $(SOURCES:.c=.o)
  EXECUTABLE = $(OBJECTS:.o=)
  
  all : $(EXECUTABLE)
  
  %.o : %.c
      @echo "Compiling c file into o file"
      ${CROSS_COMPILE}$(CC) $(CFLAGS) $&lt; -o $@
  
  % : %.o
      @echo "Linking o file into executable"
      ${CROSS_COMPILE}$(CC) $(LDFLAGS) $&lt; -o $@
  
  clean:
      rm ${EXECUTABLE}
  </code></pre>
  
  <p>This works ok then I invoke the Makefile as e.g. <code>make first.o; make first</code> or if I modify the <code>EXECUTABLE = $(OBJECTS:.o=)</code> to <code>EXECUTABLE = $(OBJECTS:.o=.out)</code> and <code>% : %.o</code> to <code>%.out : %.o</code>. If, however, I try to invoke compilation as <code>make</code> or <code>make first</code> the implicit rules are used. </p>
  
  <p>I tried going through the Makefile manual but there is really a lot of information in there, I got a little confused.</p>
  
  <p>How can I modify the Makefile to be allowed to invoke building separate targets as <code>make &lt;target&gt;</code> or all the targets as <code>make</code> as the same time?</p>