title: linux C++ gmake "No rule to make target"
body: <p>It's been a couple decades since I've had to manage makefiles.  I've checked existing answers here, as well as googling elsewhere.  When I use the target:  <code>$(BIN)/$(EXECUTABLE): $(OBJECTS)</code>
  I'm stumped as to why my makefile is failing with:</p>
  
  <p><code>$ make
  make: *** No rule to make target 'src/SRecord.o', needed by 'bin/main'.  Stop.</code></p>
  
  <p>Here's the Makefile:</p>
  
  <pre><code># execute `make D='-g'` to make with debug symbols
  DEFS := $(D)
  CC := gcc $(DEFS)
  CFLAGS := -std=c99 -Wall -Wextra
  CXX := g++ $(DEFS)
  CXXFLAGS := -std=c++17 -Wall -Wextra
  
  BIN := bin
  SRC := src
  INCLUDE := include
  LIB := lib
  
  #LIBSOURCES := $(wildcard $(LIB)/*.cpp)
  #LIBOBJECTS := $(LIBSOURCES:%.cpp=%.o)
  #OBJECTS := $(subst $(LIB)/,,$(LIBOBJECTS))
  
  SOURCES := $(wildcard $(SRC)/*.cpp)
  OBJECTS := $(SOURCES:%.cpp=%.o)
  
  # SOURCES := \
  #   $(SRC)/main.cpp \
  #   $(SRC)/SerialPort.cpp \
  #   $(SRC)/ShadowUpdate.cpp \
  #   $(SRC)/SRecord.cpp
  
  # OBJECTS := \
  #   $(SRC)/SerialPort.o \
  #   $(SRC)/ShadowUpdate.o \
  #   $(SRC)/SRecord.o
  
  EXECUTABLE := main
  
  #
  # Targets
  #
  .PHONY: clean all
  all: $(BIN)/$(EXECUTABLE)
  
  clean:
      $(RM) $(BIN)/$(EXECUTABLE)
      $(RM) $(OBJECTS)
  
  run: all
      ./$(BIN)/$(EXECUTABLE)
  
  # $(BIN)/$(EXECUTABLE): $(SRC)/*
  $(BIN)/$(EXECUTABLE): $(OBJECTS)
      $(CXX) $(CXXFLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ 
  
  #
  # Dependencies - must be at end of makefile
  #
  DEPDIR := .d
  $(shell mkdir -p $(DEPDIR) &gt;/dev/null)
  DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
  
  POSTCOMPILE = @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d &amp;&amp; touch $@
  
  %.o: %.c
  %.o: %.c $(DEPDIR)/%.d
      $(CC) -c -o $*.o $(DEPFLAGS) $(CFLAGS) $&lt;
      $(POSTCOMPILE)
  
  %.o: %.cpp
  %.o: %.cpp $(DEPDIR)/%.d
      $(CXX) -c -o $*.o $(DEPFLAGS) $(CXXFLAGS) $&lt;
      $(POSTCOMPILE)
  
  $(DEPDIR)/%.d: ;
  .PRECIOUS: $(DEPDIR)/%.d
  
  include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SOURCES))))
  </code></pre>
  
  <p>It works with the commented out target: <code># $(BIN)/$(EXECUTABLE): $(SRC)/*</code></p>
  
  <p>But, I want to be able to have a more complex project than simply listing all the source files as dependencies to the executable.</p>
  
  <p>Here's my file tree:</p>
  
  <pre><code>src/
  src/SerialPort.cpp
  src/SRecord.cpp
  src/ShadowUpdate.cpp
  src/main.cpp
  include
  include/ShadowUpdate.h
  include/SerialPort.h
  include/SRecord.h
  include/the_exo.h
  bin/
  bin/main
  Makefile
  lib/
  .d/
  </code></pre>
  
  <p>The automatic dependency generation isn't working either, even though it is straight from GNU's website.</p>