Precompiled Header Detection

Is there a way for the preprocessor to determine if the code in the current translation unit uses (or creates) precompiled headers?



The actual problem that I am currently facing is that I am in a project that is abusing PCH by pre-compiling almost all header files.
This means that there is no explicit dependency management that you can get from #includes and the compilation time is terrible. Almost every change will cause a complete restructuring.
An application is a way to do this simply to just fix it at a time, and some of the older guys refuse to believe that precompiling each of them is bad. First I need to prove it.
Therefore, I have to do this step by step and make sure that my changes do not affect the code that was compiled in the old PCH way.
My plan is to make an ifdef from PCH.h and work with the version without PCH when I have time to save.

#ifdef USES_PCH #include "PCH.h" #elif // include only whats needed #endif 

I would like to avoid defining USES_PCH on the command line and manually keep it in sync with / Y, which, in addition to being not very elegant, would be a pain. There are many configurations and modules for juggling and a large number of files that do not match the project defaults.

+4
source share
3 answers

If Visual C ++ defined a constant indicating whether precompiled headers were used, it would probably be specified in predefined macros . And it is not documented there, so it probably does not exist. (If it exists, it is probably undocumented and may change in a future version.)

+3
source

This will not work when using precompiled headers in Visual C ++; you cannot even have code before including the precompiled header. I tried to do something like this when I came across your question. After some trial and error, I found that there could be no code before the #include directive for the precompiled header when using the compiler / South option.

 #ifdef USES_PCH #include "stdafx.h" #endif 

result: fatal error C1020: unexpected #endif

+3
source

As far as I know, it cannot, but there are some heuristics: VC ++ uses StdAfx.h, Borland uses #pragma hdrstop, etc.

+1
source

All Articles