What are precompiled headers?
Often C ++ source files include headers from external libraries. On Windows, you enable windows.h . These header files can be very large and take some time to process. Each time you compile a C ++ file, the compiler must read and process thousands of lines from these header files. But external libraries do not change, and you can save a lot of time if you process these files only once and save the result.
A precompiled header is simply a bunch of header files that have been processed in an intermediate form that can later be used by the compiler again and again.
Precompiled Headers in Visual C ++
In Visual C ++, #include all your immutable header files in stdafx.h . You then instruct the compiler to create the precompiled header stdafx.pch when compiling stdafx.cpp , which does nothing but include stdafx.h . If you want to use the precompiled header in another .cpp file, you must include stdafx.h as the first include file and tell the compiler to use stdafx.pch for your precompiled header.
If you get an error that does not include stdafx.h , you just need to instruct the compiler not to use the precompiled header for this particular source file. (Or you can enable stdafx.h .)
Precompiled header settings for individual source files
Visual C ++ allows you to control compiler settings for the entire project and individual files. To access individual properties, you select the source file in the solution explorer, right-click it and select "Properties" in the context menu. Parameters for precompiled headers are located in Configuration Properties => C / C ++ => Precompiled headers. If you change these settings, you will often want to do this for all configurations (e.g. debugging and release).
When you use precompiled headers, you will have a parameter for the whole project, which instructs the compiler to use stdafx.pch for the precompiled header. stdafx.cpp will have individual settings that instruct the compiler to generate stdafx.pch , and if you have some source files that do not include stdafx.h , you can set separate settings for them so as not to use precompiled headers.
Martin Liversage Aug 18 '09 at 12:41 2009-08-18 12:41
source share