views:

1019

answers:

4

I have a sample project (not mine) which is in Visual C++ 6. I'm trying to convert it to Visual Studio 2008.

The older project is using precompiled headers. Now the questions are:

  1. What are precompiled headers?

  2. Since the older project is using precompiled headers. I'll also use them in Visual Studio 2008 (the new project). But I get errors saying that "Did you forget to include stdafx.h", to remedy the problem, I include "stdafx.h" in every source file. That worked perfectly. But the older project was not including "stdafx.h" in every file? Then how can I opt-out to include "stdafx.h" in each source file. Because not every source file need the include files defined in "stdafx.h", only few does. How is that done?

EDIT: HOW DO I EXLCUDE SOME FILES FROM USING THE PRECOMPILED HEADER?

+12  A: 

What are precompiled headers?

Often C++ source files include headers from external libraries. In Windows you include windows.h. These header files are often very large and takes some time to process. Each time you compile a C++ file the compiler has to read and process thousands of lines from these header files. But external libraries don't change and you can save a lot of time if you only process these files once and save the result.

A precompiled header is simply a bunch of header files that has been processed to an intermediate form that later can be used by the compiler again and again.

Precompiled headers in Visual C++

In Visual C++ it is customary to put all your non-changing header files in stdafx.h. You then instruct the compiler to create the precompiled header stdafx.pch while compiling stdafx.cpp. If you want to use the precompiled header in another .cpp file you have to include stdafx.h as the first include file and the instruct the compiler to use stdafx.pch for your precompiled header.

If you get an error about not including stdafx.h you simply have to instruct the compiler to not use a precompiled header for that particular source file. (Or you can include stdafx.h.)

Precompiled header settings for individual source files

Visual C++ allows you to control the compiler settings for the entire project and for individual files. To access individual properties you select the source file in the solution explorer, right click it and select Properties from the context menu. The options for precompiled headers are found at Configuration Properties => C/C++ => Precompiled Headers. If you modify these settings you will often want to do that for all configurations (e.g. Debug and Release).

When you are using precompiled headers you will have a setting for the entire project that instructs the compiler to use stdafx.pch for the precompiled header. The stdafx.cpp will have an individual settings that instructs the compiler to generate stdafx.pch, and if you have some source files that doesn't include stdafx.h you can set individual settings on these to not use precompiled headers.

Martin Liversage
thanks, that is what i was looking for
hab
+3  A: 

When you compile code, the compiler has to look in all the #included headers to know how to compile the code in your .cpp file.

With large projects (or ones using libraries like MFC) these headers can get huge, and thus take a long time to compile.

Because most of these headers don't change that often (if ever), you can get the compiler to "precompile" them - it processes them and saves its state into a precompiled header. THe next time it compiles, it doesn't need to read and compile all those headers again, so it is much faster.

One requirement 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 don't build it often, then you can just disable the "precompiled header" option (in the project settings. This applies to the whole project). The only effect you'll get is that it may compile more slowly. Or leave the option enabled and just add #include "stdafx.h" as the first include in every file.

Jason Williams
Wrong, how come in MSVC6 I don't have to include the precompiled header in every file???
hab
He(Jason Williams) is right. stdafx.h will not be included only in those files which are not using any stuff included in stdafx.h e.g., windows header files. Otherwise, look closely, stdafx.h might be included through some other include indirectly.
Aamir
A: 
  1. See MSDN
  2. Usually. you need to include "stdafx.h" in every cpp file. The whole point is that they are precompiled, and you don't need to worry that not all of them are used in some concrete file.
bocco
A: 

I've written an article on techniques that reduce the compilation time. Among these techniques a post on precompiled header and its application can be found here. It also explains how to disable precompiled headers for certain units.

Best regards, Christoph

Christoph Heindl