Weird Behavior with gcc precompiled headers

给你一囗甜甜゛ 提交于 2019-11-28 13:39:59
Basile Starynkevitch

With GCC, precompiled headers work only if they are the only header, and if they are included first (without any previous header).

This answer explains more why it is so. See also the Precompiled headers chapter of GCC documentation, which says:

  • Only one precompiled header can be used in a particular compilation.
  • A precompiled header can't be used once the first C token is seen.

BTW, it could happen that pre-compiling some large header (notably in C++) is not worth the effort. YMMV.

From the GCC manual pages:

A precompiled header can't be used once the first C token is seen.

So including <cstdio> in your precompiled header or including it first will work.

In a nutshell, the precompiled header thing works thus:

When you request to create a '.pch' file, the compiler processes the source file as usual. While it does so, its internal structures (mostly, name tables and all associated data) are populated. At the end, it makes a snapshot of these internal structures and saves it to the '.pch' file.

Later, when compiling a source file that includes a header for which a '.pch' file exists, the compiler can omit the expensive processing of the header file and load the ready-for-use snapshot from the '.pch' file instead.

Obviously, this can be done without affecting the semantics only if:

  • the inclusion directive comes before anything else;
  • the compiler options are the same.

Anything that comes before the inclusion directive may:

  • add something to the internal data structures of the compiler;
  • affect the processing of the header file;
  • alter relations between entities described there.

Therefore, in this case, loading the snapshot of internal data structures would be wrong because there'd be no guarantee that it would leave these structures in the same state as after the normal processing of the header.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!