5

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 don't follow project defaults.

4
  • Surely the preprocessor already knows whether it's supposed to be generating or using precompiled headers. It doesn't need to detect anything. Did you instead mean to ask whether the code being processed can detect it, like with some ifdef command? Commented Aug 25, 2009 at 12:45
  • Yes, I want to do a #ifdef __USES_PCH or something similar
    – grimner
    Commented Aug 25, 2009 at 13:26
  • What is the actual problem you want to solve with this?
    – peterchen
    Commented Aug 27, 2009 at 3:58
  • This answer addresses part of your question. Using those compiler switches you can remove the #include "PCH.h" directive, and create two configurations: One that does use precompiled headers (with the appropriate /FI switch), and a second configuration to use for cleanup. Commented Apr 24, 2014 at 9:54

3 Answers 3

3

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.)

1
  • 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.
    – grimner
    Commented Aug 27, 2009 at 16:22
3

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

2
  • I think #ifdef before precompiled header does work in VS2015. Commented Feb 12, 2017 at 8:52
  • @ShitalShah, it doesn't work for me in VS2015. The above code still produces a C1020 error when the file is set to use a PCH (/Yu option).
    – Matthew
    Commented Feb 13, 2017 at 20:13
1

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

2
  • VC++ only uses StdAfx.h if you work with the default project settings, you can change that at any time. Commented Aug 25, 2009 at 10:30
  • I for one change it to something unique for each vcproj, but I expect that many projects just go with the defaults.
    – rpg
    Commented Aug 25, 2009 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.