1

I'm trying to force GCC to use the precompiled header (pch.h). But I am getting an error:

.
.
.
g++ -c -std=c++20 -Wall -Wextra -Wpedantic -Wshadow -H -O3 -flto -include pch.h PeykNowruzi.cpp
! ./pch.h.gch

mingw32-make: *** [makefile:10: PeykNowruzi.o] Error 1

This is my makefile:

CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -Wpedantic -Wshadow -H -O3 -flto

all: PeykNowruzi

PeykNowruzi: PeykNowruzi.o Util.o CharMatrix.o
    $(CXX) PeykNowruzi.o Util.o CharMatrix.o -o runPeykNowruzi

PeykNowruzi.o: PeykNowruzi.cpp CharMatrix.h pch.h.gch
    $(CXX) -c $(CXXFLAGS) -include pch.h PeykNowruzi.cpp

Util.o: Util.cpp Util.h pch.h.gch
    $(CXX) -c $(CXXFLAGS) -include pch.h Util.cpp

CharMatrix.o: CharMatrix.cpp CharMatrix.h Util.h pch.h.gch
    $(CXX) -c $(CXXFLAGS) -include pch.h CharMatrix.cpp

pch.h.gch: pch.h
    $(CXX) -c $(CXXFLAGS) pch.h -o pch.h.gch

clean:
    rm *.o runPeykNowruzi

First of all, GCC does not name the header as pch.h.gch and instead it names it as pch.h. When I manually delete all the .o files and the .exe and the pch.h(the .gch one, not the .h one), then run make, it builds succesfully. But when I rename the precompiled header to pch.h.gch, it fails as I showed above. Also when I remove the pch.h, then I expect it to compile it again before compiling other source files (which depend on pch.h). I run make, it compiles pch.h, then starts to compile PeykNowruzi.cpp and fails.

I fix this problem by deleting .o and .exe and pch.h files altogether and then running make again.

Am I doing something wrong that I should not do? And why GCC on my Windows machine does not name the precompiled header as pch.h.gch?

EDIT: Regarding my 1st question, I just found out that Windows 10 File Explorer was hiding the extension of all files. I checked a box and now it shows the precompiled header as pch.h.gch.

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • Are all header files, including `pch.h`, properly guarded? – Marc Stevens Nov 28 '21 at 15:15
  • Also note that the three `.o` Makefile rules are missing a corresponding output argument `-o name.o`. – Marc Stevens Nov 28 '21 at 15:18
  • @Marc Stevens All header files have `#pragma once` at each of their first lines. – digito_evo Nov 28 '21 at 15:25
  • 1
    There is another question where it seems precompiled headers might have problems with #pragma once: https://stackoverflow.com/questions/56563679/gcc-precompiled-header-pragma-once-in-main-file You could try the usual header guards using `#ifndef`. – Marc Stevens Nov 28 '21 at 15:31
  • @Marc Stevens >"Also note that the three .o Makefile rules are missing a corresponding output argument `-o name.o`." Wow, this solved my issues! – digito_evo Nov 28 '21 at 17:09

0 Answers0