kancler
kancler

Reputation: 101

Header change doesn't trigger rebuild .o (using .d dependency file)

I have a such simple makefile:

#Compiler flags
CC := gcc
INCLDIR := -I./includes

#Directories and files
BUILDDIR := build
STLIB := $(BUILDDIR)/libbinary_tree.a

DEPS:=$(BUILDDIR)/binary_tree.d

-include $(DEPS)

$(BUILDDIR)/binary_tree.o: ./source_files/binary_tree.c
    mkdir -p $(@D)
    $(CC) -c $< $(INCLDIR) -o $@
    $(CC) -MM $(INCLDIR) ./source_files/binary_tree.c > $(BUILDDIR)/binary_tree.d

.PHONY: build clean

build: $(BUILDDIR)/binary_tree.o
    ar -rcs $(STLIB) $(BUILDDIR)/binary_tree.o

clean:
    rm -r $(BUILDDIR)

First calling make build creates ./build/binary_tree.d file, that contains binary_tree.o: source_files/binary_tree.c includes/binary_tree.h, but if I change header includes/binary_tree.h and then call make build - it doesn't rebuild build/binary_tree.o, only create library /libbinary_tree.a. I have no clue, why makefile ignores changes in the header file. Could you tell me where my logic is wrong, please?

Upvotes: 0

Views: 139

Answers (1)

MadScientist
MadScientist

Reputation: 100836

Because, binary_tree.o is not the same file as build/binary_tree.o.

So your definition of the prerequisite relationship:

binary_tree.o: source_files/binary_tree.c includes/binary_tree.h

is referring to a totally different file than the target of your compilation rule:

$(BUILDDIR)/binary_tree.o: ./source_files/binary_tree.c

Upvotes: 2

Related Questions