How to Use Pre-Compiled Headers in Vc++ Without Requiring Stdafx.H

Is there a way to use pre-compiled headers in VC++ without requiring stdafx.h?

Yes, there is a better way.

The problem, IMHO, with the 'wizard style' of precompiled headers is that they encourage unrequired coupling and make reusing code harder than it should be. Also, code that's been written with the 'just stick everything in stdafx.h' style is prone to be a pain to maintain as changing anything in any header file is likely to cause the whole codebase to recompile every time. This can make simple refactoring take forever as each change and recompile cycle takes far longer than it should.

A better way, again IMHO, is to use #pragma hdrstop and /Yc and /Yu. This enables you to easily set up build configurations that DO use precompiled headers and also build configurations that do not use precompiled headers. The files that use precompiled headers don't have a direct dependency on the precompiled header itself in the source file which enables them to be build with or without the precompiled header. The project file determines what source file builds the precompiled header and the #pragma hdrstop line in each source file determines which includes are taken from the precompiled header (if used) and which are taken directly from the source file... This means that when doing maintenance you would use the configuration that doesn't use precompiled headers and only the code that you need to rebuild after a header file change will rebuild. When doing full builds you can use the precompiled header configurations to speed up the compilation process. Another good thing about having the non-precompiled header build option is that it makes sure that your cpp files only include what they need and include everything that they need (something that is hard if you use the 'wizard style' of precompiled header.

I've written a bit about how this works here: http://www.lenholgate.com/blog/2004/07/fi-stlport-precompiled-headers-warning-level-4-and-pragma-hdrstop.html (ignore the stuff about /FI) and I have some example projects that build with the #pragma hdrstop and /Yc /Yu method here: http://www.lenholgate.com/blog/2008/04/practical-testing-16---fixing-a-timeout-bug.html .

Of course, getting from the 'wizard style' precompiled header usage to a more controlled style is often non-trivial...

Is there any way to use VC++ 2010 without including stdafx.h?

It's for pre-compiled headers. Don't use pre-compiled headers, don't include it.

Should stdafx.h/.cpp be removed from a static library project in C++?

Compiling even a small project may take a lot of time without precompiled headers because any project indirectly includes many SDK and standard library headers. So yes, you need those files and no, you won't get anything useful by removing them. Your project will compile very slowly without them, that's the only result you will get.

In case you don't know this already - all includes for standard header files like 'windows.h' or 'stdlib.h' must be in 'stdafx.h'. This way the standard headers will not be recompiled on every project build (and for every project file).

Can force include be used with pre-compiled headers in Visual C++?

The problem is that stdafx.h is not C:\path\to\stdafx.h. VC++ does a string compare. You'd need to add C:\path\to\ to the include path, so you can use just /FI"stdafx.h"

Precompiled Headers in Header Files

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.

stdafx.h: When do I need it?

If you don't want to use precompiled headers, then there is no point to using a standard include file - this will slow down the build for every file that includes it and cause them to include extra stuff that they do not need. Get rid of it and just include the headers they need.



Related Topics



Leave a reply



Submit