PCH + gcc: sccache fails, ccache succeeds

I discovered today that if using gcc with precompiled headers, using sccache fails but ccache works. It appears that sccache doesn’t properly handle the PCH artifacts, resulting in errors of the form:

cc1plus: error: /some/path/cmake_pch.hxx.gch: not a PCH file

even though that file does exist and was created by gcc, so it’s presumably a valid PCH file.

@ben.boeckel reported sccache not supporting PCH years ago in Does not support PCH compilation using `-include` · Issue #615 · mozilla/sccache · GitHub, which is still an open issue, but I don’t know if it is essentially the same lack of support, just a different manifestation of what I’m seeing. Anyone have any further details on this particular combination and whether it’s likely to be fixed any time soon? I was surprised by how little there seems to be online about it, given that it makes sccache a non-starter for any project using gcc with precompiled headers. For CMake projects, I’d expect that to be a common enough combination that we should be seeing more bug reports and discussions about it, so maybe I’m missing some important detail.

Strangely, it seems that sccache does handle clang + PCH.

I think PCH usage is actually way lower in practice. But it seems that GCC’s -include usage for PCH is different enough from Clang’s. -include is seen as a preprocessor argument, but is needed for the compile with GCC"s PCH. How does Clang do PCH?

I don’t know the details, but maybe @cristianadam does (he implemented the PCH feature, from what I recall).

This is how GCC does PCH, and how Clang does it.

I think people do not complain about it because the usual combination is cache without PCH. This is how Qt does it.

Here is the ccache manual documentation about PCH.

I personally prefer ccache as seen at gitlab-ci: consider using ccache instead of sccache (#23877) · Issues · CMake / CMake · GitLab (kitware.com) :slight_smile: