title: GNU make implicit archive rule with stem
body: <p>I am trying to setup a Makefile to build either static (.a) and dynamic (.so) libraries depending on the target file extension.</p>
  <p>I previously used the following Makefile for static libraries only :</p>
  
  <pre class="lang-make prettyprint-override"><code>NAME   := config
  LIB    := lib$(NAME).a
  SRC    := $(wildcard *.c)
  OBJ    := $(SRC:.c=.o)
  CFLAGS += -W -Wall
  
  .PHONY: all clean fclean re
  
  all: $(LIB)
  
  clean:
      @$(RM) $(OBJ)
  
  fclean: clean
      @$(RM) $(LIB)
  
  re: fclean all
  
  $(LIB): $(LIB)($(OBJ))
      ranlib $@
  </code></pre>
  <p><strong>My main goal is to be able to compile multiple libraries by only changing the <code>LIB</code> and <code>NAME</code>variables.</strong></p>
  <p>Everything worked fine, so I added the following for the dynamic libraries :</p>
  <pre class="lang-make prettyprint-override"><code>LDFLAGS += -shared
  
  %.so: CFLAGS += -fPIC
  %.so: $(OBJ)
      $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
  </code></pre>
  <p>And changed the <code>$(LIB):</code> rule by the following generic rule :</p>
  <pre class="lang-make prettyprint-override"><code>%.a: %.a($(OBJ))
      ranlib $@
  </code></pre>
  <p>If I change <code>LIB</code> to <code>lib$(NAME).so</code> everything works as expected, but with the <code>.a</code> extension, make prints me this error :</p>
  <p><code>make: *** No rule to make target 'libconfig.a', needed by 'all'.  Stop.</code></p>
  <p>The only solution I found was to add <strong>another explicit rule</strong> like that :</p>
  <pre class="lang-make prettyprint-override"><code>%.a($(OBJ)): $(OBJ)
      $(AR) rv $@ $^
  </code></pre>
  <p>And now everything works.</p>
  <p>But adding this explicit rule prevents me from relying only upon GNU make's implicit rules, and makes me call explicitly ar, which I wanted to avoid.</p>
  <p>Is this some kind of bug or am I missing something ?</p>
  <pre class="lang-make prettyprint-override"><code>GNU Make 3.82
  Built for x86_64-unknown-linux-gnu
  </code></pre>