Precompiled Headers

I have an example project (not mine) that is in Visual C ++ 6. I am trying to convert it to Visual Studio 2008.

An older project uses precompiled headers. Now questions:

  • What are precompiled headers?

  • Because an older project uses precompiled headers. I will also use them in Visual Studio 2008 (new project). But I get errors saying that “Did you forget to include stdafx.h” to fix the problem, I include “stdafx.h” in every source file. It worked perfectly. But did the old project not include "stdafx.h" in every file? Then how can I refuse to include "stdafx.h" in every source file. Because not every source file needs include files defined in "stdafx.h", only a few do. How it's done?

EDIT: HOW DO I CONTINUE SOME FILES FROM USING THE PRELIMINARY HEAD?

+31
visual-studio-2008 vc6 precompiled-headers
Aug 18 '09 at 12:32
source share
3 answers

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.

+55
Aug 18 '09 at 12:41
source share

When compiling the code, the compiler should look in all # headers to know how to compile the code in your .cpp file.

With large projects (or using libraries such as MFC) these headers can become huge and thus take a long time to compile.

Since most of these headers often do not change (if ever), you can force the compiler to “precompile” them - it processes them and saves its state into a precompiled header. The next time it compiles, it no longer needs to read and compile all these headers, so it is much faster.

One of the requirements in Visual Studio is that if you use a precompiled header, it must be included in every file in the project.

If the project is small or you often do not create it, you can simply disable the “precompiled header” option (in the project settings. This applies to the entire project . ). The only effect you get is that it can compile more slowly. Or leave this option enabled and just add #include "stdafx.h", as the first ones are included in every file.

+4
Aug 18 '09 at 12:41
source share
  • See MSDN
  • Usually. you need to include "stdafx.h" in every cpp file. The thing is that they are precompiled, and you do not need to worry about the fact that not all of them are used in a particular file.
0
Aug 18 '09 at 12:42
source share