title: Using sed in a makefile; how to escape variables?
body: <p>I am writing a generic makefile to build static libraries. It seems to work well so far, except for the line calling sed:</p>
  
  <pre><code># Generic makefile to build a static library
  ARCH       = linux
  
  CFLAGS     = -O3 -Wall
  
  SOURCES    = src
  BUILD_DIR  = build/$(ARCH)
  TARGET     = $(BUILD_DIR)/libz.a
  
  CFILES     = $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.c))
  OBJECTS    = $(addprefix $(BUILD_DIR)/,$(CFILES:.c=.o))
  
  # Pull in the dependencies if they exist
  # http://scottmcpeak.com/autodepend/autodepend.html
  -include $(OBJECTS:.o=.dep)
  
  default: create-dirs $(TARGET)
  
  $(TARGET): $(OBJECTS)
      $(AR) -rc $(TARGET) $^
  
  $(BUILD_DIR)/%.o: %.c 
      $(CC) $(CFLAGS) -c $&lt; -o $@ 
      $(CC) -M $(CFLAGS) $*.c &gt; $(BUILD_DIR)/$*.tmp
      sed s/.*:/$(BUILD_DIR)\\/$*.o:/ $(BUILD_DIR)/$*.tmp &gt; $(BUILD_DIR)/$*.dep
      @rm $(BUILD_DIR)/$*.tmp
  
  .PHONY: create-dirs
  create-dirs:
      @for p in $(SOURCES); do mkdir -p $(BUILD_DIR)/$$p; done
  
  .PHONY: clean
  clean:
      rm -fr $(BUILD_DIR)
  </code></pre>
  
  <p>sed is used to replace the path/name of the object file with the full path of where the object actually is. e.g. 'src/foo.o:' is replaced with 'build/linux/src/foo.o:' in this example.
  $(BUILD_DIR) and $* in the replacement string both contain forward slashes when expanded - how do I pass them to sed?  </p>
  
  <p>Note: This might have been answered here before, but I am so far unable to apply those answers to my specific problem!</p>