I'm trying to use precompiled headers with GCC to speed up the compilation process. If I launch the compilation directly from the command line the precompiled headers are used, but if I try to organize the compilation using a makefile they are not.
More specifically, I try to compile with GCC 8.1.0 a file main.cpp using a precompiled header lib.hpp.gch for the file lib.hpp included as first token in main.cpp.
lib.hpp is precompiled with
$ g++ -O2 -H -Wall -std=c++17 -c lib.hpp
main.cpp is then compiled with
$ g++ -O2 -H -Wall -std=c++17 -c main.cpp -o main.o
! lib.hpp.gch
...
and I can see from the "!" that the precompiled lib.hpp.gch is actually used.
If I write a makefile for this
CXX = g++
CXXFLAGS = -O2 -H -Wall -std=c++17
main.o: \
main.cpp \
main.hpp \
lib.hpp
$(CXX) $(CXXFLAGS) \
-c main.cpp \
-o main.o
and then use make, I would expect the same usage of the precompiled header
but instead it fails, as can be seen from the "x":
$ make
g++ -O2 -H -Wall -std=c++17 \
-c main.cpp \
-o main.o
x lib.hpp.gch
...
This is very strange, because the command issued by make seems exactly the same as the one that I used manually before.
I've also made measurement of timings and can confirm that the compilation via make is definitely slower than the manual one, confirming that the precompiled header is not used.
What's wrong in the makefile?