views:

268

answers:

3

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

---
The actual problem I'm facing right now is that I'm on a project that is abusing PCH by precompiling virtually all header files.
That means there is none of the clear dependency management you can get from #includes and the compile times is awful. Practically every change will trigger a full rebuild.
The application is way to big to just fix it in one go, and some of the old guys refuses to belive that precompiling everyting is bad in any way. I will have to prove it first.
So I must do it step by step and make sure my changes does not affect code that is compiled the old PCH way.
My plan is to do ifdef out the PCH.h and work on the non PCH version whenever I have some time to spare.

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

I would like to avoid defining USES_PCH at command line and manually keep it in sync with /Y that, besides from not being very elegant, would be a pain. There is a lot of configurations and modules to juggle with and a lot of files that dont follow project defaults.

+1  A: 

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

rpg
VC++ only uses StdAfx.h if you work with the default project settings, you can change that at any time.
Timo Geusch
I for one change it to something unique for each vcproj, but I expect that many projects just go with the defaults.
rpg
+2  A: 

If Visual C++ defined a constant to indicate whether precompiled headers were in use, it would probably be listed in Predefined Macros. And it's not documented there, so it probably doesn't exist. (If it does exist, it's probably undocumented and may change in a future version.)

bk1e
Yes I know the is no official macro. If there was one I wouldn't have to ask here.I hoped that someone knew of some indirect way of figuring it out anyway.
A: 

This will not work, when using precompiled headers in Visual C++, you cannot even have any code before including a precompiled header. I was trying to do something similar, when I came across your question. After a little trial and error, I have found that there can be no code prior to the #include directive for the precompiled header when using the /Yu compiler option.

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

result: fatal error C1020: unexpected #endif

imafishimafish