Paweł Pela
Paweł Pela

Reputation: 451

Why make keeps recompiling all the objects?

I've been trying to make this Makefile to work at least three times now. With each iteration I am getting closer to my goal, fixing some previous problems while re-reading the official GNU docs. Now I'm left with only one place which I don't understand.

Here's the Makefile:

MAKEFLAGS=-j2
CC=clang++
OPTIMALIZATION=-O2
CFLAGS=-c -std=c++11 -gsplit-dwarf -I. -Iengine -Iengine/bootstrap
LDFLAGS=-lglut -lGL -lGLU -lGLEW -lSOIL -lassimp
SOURCES=$(wildcard *.cpp engine/*.cpp engine/lighting/*.cpp engine/bootstrap/*.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=$(addprefix build/,$(shell basename ${PWD}))

.PHONY: depend clean

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(addprefix build/,$(notdir $(OBJECTS))) -o $@ $(LDFLAGS)

.cpp.o:
    $(CC) $(CFLAGS) $< -o build/$(notdir $@)

run: all
    ./$(EXECUTABLE)

clean:
    rm -rf build/*

depend: $(SOURCES)
    makedepend $^

# DO NOT DELETE

My goal is to build all the files into ./build directory. And I recently managed to do that with some file name functions, as the GNU docs describe.

But now every time I run make or make run, it recompiles the entire project.

How can I fix it to work only for files that have been changed recently?

Upvotes: 1

Views: 160

Answers (2)

marom
marom

Reputation: 5230

I think you need to set OBJECTS like this

OBJECTS=$(addprefix ./build/, $(SOURCES:.cpp=.o))

instead of

OBJECTS=$(SOURCES:.cpp=.o)

to tell makefile that objects are in ./build directory. Currently you are just telling $(CC) to place outputs in ./build ( -o build...), but when it needs to check if OBJECTS are up to date the check fails, since it looks for objects in current dir and so retriggers compilation.

Once this is done you can remove the addprefix from the linker command.

Upvotes: 2

ern0
ern0

Reputation: 3172

The .cpp.o: looks weird in your makefile. Probably an implicit rule runs instead of it (see https://www.gnu.org/software/make/manual/html_node/Using-Implicit.html#Using-Implicit ). You may add some echo command to maks sure wheter your compiler command runs, or the implicite one.

Upvotes: 0

Related Questions