C ++ compiler macro for "Use precompiled headers" state

Does it have a predefined C ++ compiler macro that I can use to determine if a file is compiled using Use Precompiled Headers, Create Precompiled Headers, Do Not Use Precompiled Headers?

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

+2
source share
5 answers

I do not think that there is something, although I, of course, understand the desire for this. Whenever I have to build my cross-platform library on a system that does not support PCH, it takes forever, since a lot of files pull in the path more than they really need, and it would be nice to trim it. Unfortunately, I cannot because of how Visual Studio handles PCH. Namely, the inclusion of PCH should be the first non-commentary file. From the way you formulated your question, I suspect that you are also working with Visual Studio.

I'm not sure if this will work for you, but you can 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 is included in the kit, and the right header protectors, along with optimizations in most modern compilers to detect header protection, can reduce build time. You can change the macro definition for an individual file in the build settings of your project, in a similar estate, as you can change the PCH settings for each file.

Although I must admit that I'm not sure what you are trying to do, and I suspect that this is really an XY problem

+2
source

Visual Studio / MSC does not provide a predefined macro that transfers the /Y[-cdu] compiler setting for verification from source code.

However, there is a solution to the problem you are trying to solve, i.e. control whether there should be the first line without comment in the source file #include "<my pch.h>" : MSC offers the / FI (force file inclusion name) compiler.

This parameter has the same effect as specifying a file with double quotes in the #include directive on the first line of each source file specified on the command line [...]

This compiler can be specified on the compiler command line or on the basis of each project through the IDE GUI (Project → Properties: C / C ++ → Advanced: Forced Include File).

With the combination of the /Y[-cdu] and /FI switches, you can control usage and meet the requirements for using precompiled headers outside of the source code.

+1
source

In this case, I think you can create a macro yourself. You can define USE_PRECOMPILEDHDR and FORCED_INCLUDEHDR when you use precompilation like this

 #if USE_PRECOMPILEDHDR #ifndef FORCED_INCLUDEHDR #include "stdafx.h" #endif #else //..manualy include all your headers #endif 

But, like another statement, except when you change another compiler, you have no reason to use protective measures for this.

0
source

This feature is unlikely to exist. The whole point of precompiled headers is that headers will be compiled with exactly the same compiler options as when compiling for real. If the compiler suggested a way for your code to talk about the difference, then you could make your code behave differently (at the preprocessor level) depending on whether the compiler pre-compiles or compiles.

-1
source

If you want to include header files depending on whether precompiled headers are included, use the Include Guard instead.

-3
source

All Articles