In my project I have some header files which contain methods (e.g. for template classes). All these header files are included in a single file header.h
, which is then included in every cpp file. This way, I have to change the code just in one place. There are also some .h
files without a corresponding .cpp
file.
Then I have this makefile:
# Makefile
.PHONY: run clean rebuild
CC = g++
CFLAGS = -Wall -Ofast -std=c++0x -pthread
RM = rm -f
EXEC = main
SRC = $(wildcard *.cpp)
OBJ = $(SRC:.cpp=.o)
$(EXEC): $(OBJ)
$(CC) $(CFLAGS) -o $@ $(OBJ)
%.o: %.cpp
$(CC) $(CFLAGS) -c $^
run: $(EXEC)
./$(EXEC)
clean:
$(RM) $(EXEC) *.o *.gch *~
rebuild: clean $(EXEC)
Everything works fine, except for one small but annoying detail: if I modify a cpp file, then I can do make
and everything is updated correctly, but if I modify a header file then I have to remove everything and recompile from scratch (and this is the reason why I have that ugly rebuild
target), otherwise the edit will have no effect.
Is there a way to make things better without restructuring the entire code?
EDIT
I tried with this makefile
.PHONY: run clean rebuild
CC = g++
CFLAGS = -Wall -Ofast -std=c++0x -pthread
RM = rm -f
EXEC = main
SRC = $(wildcard *.cpp)
OBJ = $(SRC:.cpp=.o)
$(EXEC): $(OBJ)
$(CC) $(CFLAGS) -o $@ $(OBJ)
%.o: %.cpp headers.h
$(CC) $(CFLAGS) -c $<
run: $(EXEC)
./$(EXEC)
clean:
$(RM) $(EXEC) *.o *.gch *.d *~
rebuild: clean $(EXEC)
but the result is not what I want: if I modify a single header file and the do make
, it tells me that the target is up to date, while I would like it to be recompiled.