Fredrik Paulsson
Fredrik Paulsson

Reputation: 21

Make won't recompile header file changes. Despite including .d dependency files

I have a really strange issue with the Makefile in one of my off-time projects.

I have a Makefile (shown below) that generates dependency information in .d files following https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html.

The problem is that a change in one of my header files (shader.h) does not trigger the recompilation of shader.o (out of shader.cpp).

The problem started happening recently when I re-organized the directory structure in my project so I suspect it is related to that.

The strange thing is that if I do make --print-data-base it does seem like it has found the correct prerequisites matching the shader.d file.

The diretory structure is as follows:

|-src
  |-engine
    |-shader.h
    |-shader.cpp
|-bin
  |-Debug
    |-OpenGLTest
|-obj
  |-Debug
    |-engine
      |-shader.o
|-dep
  |-engine
    |-shader.d

Makefile:

WORKDIR = `pwd`

CC = gcc
CXX = g++
AR = ar
LD = g++
WINDRES = windres

INC = -I/usr/local/include
CFLAGS = -Wall -Werror
CXX_FLAGS = -std=c++11
RESINC =
LIBDIR = -L/usr/local/lib
LIB = -lSDL2 -lGLEW -framework OpenGL
LDFLAGS =

DEPDIR = dep
SRCDIR = src

INC_DEBUG = $(INC)
CFLAGS_DEBUG = $(CFLAGS) -g
RESINC_DEBUG = $(RESINC)
RCFLAGS_DEBUG = $(RCFLAGS)
LIBDIR_DEBUG = $(LIBDIR)
LIB_DEBUG = $(LIB)
LDFLAGS_DEBUG = $(LDFLAGS)
OBJDIR_DEBUG = obj/Debug
DEP_DEBUG =
OUT_DEBUG = bin/Debug/OpenGLTest

CXX_SRCS = $(wildcard $(SRCDIR)/*.cpp) $(wildcard $(SRCDIR)/**/*.cpp)
CXX_REL_SRCS = $(subst $(SRCDIR)/,,$(CXX_SRCS))
OBJS = $(CXX_REL_SRCS:%.cpp=%.o)

OBJ_DEBUG = $(addprefix $(OBJDIR_DEBUG)/,$(OBJS))

# ----------------------------- debug -----------------------------

clean: clean_debug clean_release
    rm -rf $(DEPDIR)

before_debug:
    @test -d bin/Debug || mkdir -p bin/Debug
    @test -d $(OBJDIR_DEBUG) || mkdir -p $(OBJDIR_DEBUG)
    @mkdir -p $(dir $(OBJ_DEBUG))

after_debug:

debug: before_debug out_debug after_debug

out_debug: before_debug $(OBJ_DEBUG) $(DEP_DEBUG)
    $(LD) $(LIBDIR_DEBUG) -o $(OUT_DEBUG) $(OBJ_DEBUG)  $(LDFLAGS_DEBUG) $(LIB_DEBUG)

$(OBJDIR_DEBUG)/%.o: $(SRCDIR)/%.cpp
    $(CXX) $(CFLAGS_DEBUG) $(CXX_FLAGS) $(INC_DEBUG) -c $< -o $@

clean_debug:
    rm -f $(OBJ_DEBUG) $(OUT_DEBUG)
    rm -rf bin/Debug
    rm -rf $(OBJDIR_DEBUG)

# ----------------------------- dependencies -----------------------------

# Generate dependencies in *.d files
$(DEPDIR)/%.d: $(SRCDIR)/%.cpp
    @test -d $(DEPDIR) || mkdir -p $(DEPDIR)
    @mkdir -p $(dir $@)
    @set -e; rm -f $@; \
         $(CXX) -MM $(CFLAGS) $(CXX_FLAGS) $(INC) $< > $@.$$$$; \
         sed 's,\(.*\)\.o[ :]*,$(OBJDIR_RELEASE)/\1.o $(OBJDIR_DEBUG)/\1.o $@ : ,g' < $@.$$$$ > $@; \
         rm -f $@.$$$$

# Include the *.d files
include $(patsubst %,$(DEPDIR)/%.d,$(basename $(CXX_REL_SRCS)))

# ----------------------------- targets -----------------------------

.PHONY: before_debug after_debug clean_debug

all: debug

shader.d:

obj/Debug/shader.o dep/engine/shader.d : src/engine/shader.cpp src/engine/shader.h \
src/engine/transform.h src/engine/camera.h src/engine/constants.h

Upvotes: 0

Views: 122

Answers (1)

Fredrik Paulsson
Fredrik Paulsson

Reputation: 21

I'll answer this since the issue has been found by @G.M. in the comments.

Turns out that the dependency generation was flawed.

In shader.d obj/Debug/shader.o should really be obj/Debug/engine/shader.o. Modifying the sed command as shown below fixes this.

sed 's,\(.*\)\.o[ :]*,$(OBJDIR_RELEASE)/$(subst $(SRCDIR)/,,$(dir $<))\1.o $(OBJDIR_DEBUG)/$(subst $(SRCDIR)/,,$(dir $<))\1.o $@ : ,g' < $@.$$$$ > $@;

Upvotes: 2

Related Questions