Johannes Gerer Johannes Gerer - 1 year ago 103
C++ Question

C++ Compiler Macro for Status of "Use Precompiled Headers"

It there a predefined c++ compiler macro that I can use to tell, whether a file is compiled with "Use Precompiled Headers", "Create Precompiled Headers", "Dont Use Precompiled Headers"?

See @IronMensan 's answer for the purpose of such a macro!

Answer Source

I don't think there is anything, though I certainly understand the desire for one. Whenever I have to build my cross-platform library on a system that dozen't support PCH, it takes forever since a lot of files are pulling in way more than they really need and it would be nice to trim that out. Unfortunately I can't because of how Visual Studio handles PCH. Namely that the inclusion of the PCH must be the first non-comment line of the file. From the way you worded your question, I suspect that you are also working with Visual Studio.

I am not sure if this will work for you but you could try something like this:

#include MY_PCH_FILE

And use /DMY_PCH_FILE="myfile.h"

on the command line to control what the first include file is. After that you have full control over what gets included and proper header guards along with the optimization in most modern compilers to detect header guards could reduce build times. You can change the definition of the macro for individual file in the build settings of your project, in a similar manor to how you can change the PCH settings for each file.

Though I must admit that I am not sure what you are trying to do and I suspect this is really an XY problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download