Setting up a project that will compile with or without a precompiled header

What I'm trying to do is create a project in which it doesn't matter if the precompiled header is set or not. stdafx.hpp - my PH

My main problem is that I have files through server directories, not just the project root directory. What I tried:

  • If I

     #include "stdafx.hpp" 

    everywhere works fine with setting PH, but it will complain if I deactivate it, because the files in the subdirectories will not find it.

  • If I

     #include "../stdafx.hpp" 

    in files located in subdirs, it works fine without PH, but with PH, the compiler complains about stdafx.hpp does not turn on.

  • If I set the power, include the file in stdafx.hpp , either with a relative or an absolute path, the compiler gave me errors (I can’t remember right now, if necessary, I will reproduce it).

So what could be the solution?

+5
source share
2 answers

The canonical solution is easy: do not include the precompiled header file (default is stdafx.h). If your code needs to be compiled with precompiled headers, use the / FI (Name Forced Include File) compiler:

This option has the same effect as specifying a double-quoted file in the #include directive on the first line of each source file specified on the command line, in a CL environment variable, or in a command file.

This allows you to use precompiled header files without changing the source code.

Rules for using the #include directive with double quotes are described in # include Directive (C / C ++) :

Quote form:

The preprocessor searches for included files in the following order:

  • In the same directory as the file containing the #include statement.
  • In directories of open include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues up through the directories of any files with grandparents.
  • The path specified by each / I compiler option.
  • Along the way, set by the INCLUDE environment variable.

Using / I (optional Include directories) , the compiler will include the directory of the header file used to generate the precompiled header, then it’s just possible to write

 /FIstdafx.hpp 

There is no combination of project settings / topology that allows you to enable or disable precompiled headers. The compiler switches / Y , / FI and / I must be used together or completely removed. To change a set of configurations as a unit, you can use property pages (for more details see Working with Project Properties ).

+6
source

A simple solution is to indicate that your project includes paths, so that the simple #include "stdafx.hpp" always finds the header, regardless of whether it includes the file directory.

If you are worried about inadvertently creating header files in other directories, you can even put a potentially precompiled header in your own special directory.

+1
source

All Articles