15

I ran into precompiled headers today for the first time..forever changing my life. I can't believe compiling my C++ code could be that fast. It makes total sense now..

Anyway, one thing that is confusing me is that from what I've read so far, pre-compiled headers only should be added to source files( cpp? ).

In Visual Studio, there is an option under Project Properties->C/C++->Advanced to "Force Include File". I set that compiler option to stdafx.h.

After doing this..I no longer require to include the headers I have added to my stdafx.h, even inside my header files ( source files are supposed to automatically include stdafx.h ). Is this expected behaviour?

I can't find a place that's clear in the distinction between header/source files.

If it does..great but I'm afraid it's another one of those things VC++ lets you get away with but will break in GCC. And yes..it needs to be portable; at least between GCC and VC++.

Filburt
  • 17,626
  • 12
  • 64
  • 115
irwinb
  • 993
  • 2
  • 10
  • 20
  • 1
    Well, if you want your project to work on gcc as well then you'd better stay away from the "Force Include File" option. Boilerplate is to make the first #include in your source code file the precompiled header file. – Hans Passant Jul 09 '12 at 21:15
  • 2
    @Hans, the equivalent gcc feature would be to use the `-include` command-line option to include *stdafx.h* at the start of every file. – Rob Kennedy Jul 09 '12 at 21:21

3 Answers3

22

StdAfx.h really should only be included in source files, not headers. I would suggest you #include "StdAfx.h" first in every cpp and not use the "Force Include File" option. Thats how I do it with my cross-platform projects. For the record, I don't actually use precompiled headers in GCC I just build it normally and it works well.

For some background. The compiler only looks at source files (ie, *.cpp, *.c, etc) and so when it compiles them it has to include every header and compile any code found in the headers as well. The precompiled headers option allows for compiling all of that code (ie, the globally include'd code in StdAfx.h) once so that you don't have to do it all of the time. Thats what StdAfx.cpp is for. The compiler compiles StdAfx.cpp with all of the code included in StdAfx.h once instead of having to do it every time you build.

So, since you include StdAfx.h in every source file as the first item, it doesn't make sense to include it in any of the headers since they will be included AFTER StdAfx.h and thus will have access to all of the code in StdAfx.h. Plus you can then use those headers in other projects without having to worry about having a StdAfx.h around or including the wrong one.

syplex
  • 1,147
  • 6
  • 27
  • My question was that does VC++ add these automatically to the headers? I get no errors when using types that I have not included ( but are in the stdafx.h)..very weird. – irwinb Jul 11 '12 at 04:09
  • 3
    No, the force include option adds the file to the first line of every source/cpp file. The way includes work is the contents of the file to be included get placed into the source file at the location of the #include. With the force include option that would make every CPP file have the contents of your StdAfx.h at the top, followed by the contents of all of your header files and source code (in whatever order it is in the file). So the effect is, everything defined in your StdAfx.h will be available in your headers, within that project. The compiler only looks at source files not headers. – syplex Jul 13 '12 at 21:30
  • Oh I see. That makes perfect sense. Thank your explaining that. – irwinb Jul 19 '12 at 18:19
6

Yes, it is expected behaviour. The Project Properties->C/C++->Advanced to "Force Include File" setting controls Visual C++ compiler option /FI:

This option has the same effect as specifying the file with double quotation marks in an #include directive on the first line of every source file

So, it frees you from including the stdafx.h manually.

Although, you can use precompiled headers with GCC and other compilers The Visual C++'s shortcut behaviour is not portable across other compilers. So, check How to handle stdafx.h in cross-platform code? where ideas for portable solutions are discussed.

Long story short, include stdafx.h manually in your .cpp source files and you should be fine also with GCC (assuming, you will configure your build for GCC to use precompiled headers).

Community
  • 1
  • 1
mloskot
  • 37,086
  • 11
  • 109
  • 136
  • Ads to both the .h and .cpp files? – irwinb Jul 09 '12 at 21:40
  • If it only adds to .cpp files, why is VC++ ok with me having deceleration of those types in my header files without included them? – irwinb Jul 10 '12 at 16:21
  • @irwinb You add common headers to stdafx.h for both, Visual C++ and GCC. Then you include stdafx.h in your .cpp files. But, if you used VC++ only, then you could specify stdafx.h with /FI option requesting the compiler to automatically include this header for you. – mloskot Jul 11 '12 at 09:09
3

Do not use the "Force Include File" setting (/FI) as it breaks Edit & Continue ! (and MS doesn't seem to want to fix this issue)

See https://connect.microsoft.com/VisualStudio/feedback/details/668339/vs-2010-sp1-c-edit-and-continue-fails-with-fi

and https://connect.microsoft.com/VisualStudio/feedback/details/342441/visual-studio-2005-force-includes-breaks-edit-and-continue-with-pre-compiled-headers

#include "stdafx.h" should only be found as the first non-comment line in your source files, not in header files.

Wizou
  • 1,336
  • 13
  • 24