Search code examples
c++makefileprecompiled-headers

Can a pch show warnings referencing the textual source?


I've been compiling a C++ project with a chained pre-compiled header file (.pch). I am using the Clang compiler on a Linux platform, with a few warning flags, ex: -pedantic, -Wall.

Now, upon compiling the .pch, no warnings are shown at all, but, when compiling my main source code (.cpp), a few warnings are logged to the terminal showing actual locations from the textual source.

(For clarification, the warnings are from the original .pch source)

I could just disable the warnings, but I'm confused on one point: how could the compiler log text to the terminal if the .pch is a binary file? I am thinking that the compiler is somehow using the textual source, instead of using the .pch at all.

Would anyone know anything that would counter my presumption?

If it helps, my makefile is roughy laid out like so:

foo = ./somewhere/foo
bar = ./somewhere/bar
main ./main.cpp
output_file = ./app

flags = -pthread -Wall -pedantic -std=c++2a -O2

$(output_file): $(main) $(foo).pch
    clang++ \
        $(flags) \
        -include-pch $(foo).pch \
        -fsanitize=$(sanitize) \
        $(main) \
        -o $(output_file)

$(foo).pch: $(foo).hpp $(bar).pch
    clang++ $(flags) -include-pch $(bar).pch -x c++-header $(foo).hpp -o $(foo).pch

$(bar).pch: $(bar).hpp
    clang++ $(flags) -x c++-header $(bar).hpp -o $(bar).pch

Solution

  • PCH doesn't mean it's compiled, it's only "precompiled" which is only a fancy name for saying "it's a dump of the current parser state".

    Meaning preprocessing has been performed (includes, ifdefs and macros are gone), and the code has been cut into tokens, but no actual translation work has been performed yet.

    All that, and that includes anything which could generate warnings on anything but basic syntax errors, only happens once compilation starts.

    And that only happens once the lexer has resumed work, by being fed the main source file as additional input, continuing off where it got to when creating the PCH.