HOWTO: Using .pch Files Across Projects in the Visual Workbench (137515)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Editions 2.0
  • Microsoft Visual C++, 32-bit Editions 2.1
  • Microsoft Visual C++, 32-bit Editions 2.2
  • Microsoft Visual C++, 32-bit Editions 4.0

This article was previously published under Q137515

SUMMARY

In some cases, it is beneficial to share precompiled header (.pch) files among several projects. However, the Visual Workbench can't use the program database (.pdb) files because project-specific information related to each .pch file is stored in the .pdb files. This article explains how to use .pch files without using .pdb files in the versions of Visual C++ listed at the beginning of this article.

For information about about the 16-bit Visual Workbench, please see the following article in the Microsoft Knowledge Base:

117780 Using .PCH Files Across Projects in Visual Workbench

MORE INFORMATION

Step-by-Step Procedure

To use a common precompiled header among different projects, first determine which header file you wish to use. (If all the projects have been generated by AppWizard, the stdafx.h file is the same for each, so it is a good candidate for a shared precompiled header.) Once you've made your choice, copy the header file into a common directory.

Now, for each project that will share the precompiled header, do the following:

  1. Delete the shared header file from the individual project directories.
  2. For each .cpp file that includes the file being used for shared precompiled header, specify an explicit path to the header. For example, suppose that stdafx.h is the designated to be the shared header file and a .cpp file contains the following:
          #include "stdafx.h"
    						
    If the new location of the header file is c:\common\headers, then the above statement in the .cpp file should be changed to the following:
          #include "c:\common\headers\stdafx.h"
    						
  3. With Visual C++ 2.x, choose Settings from the Project menu. With Visual C++ 4.0, choose Settings from the Build menu.
  4. Click the C/C++ tab.
  5. Choose Precompiled Headers from the Category list box.
  6. With Visual C++ 2.x, in the Precompile through Header edit box, type the complete path of the common header. With Visual C++ 4.0, select Automatic use of Precompiled Headers and specify the complete path to the header file in the enabled Through header edit control. (The path to use is the same path specified in Step 2 above.)
  7. Click the C/C++ tab, and choose General from the Category list box.
  8. Choose the "C7 Compatible" from the "Debug Info" List box. This tells the compiler not to place any of the precompiled header information into the .pdb file, which keeps inconsistency errors from occurring in that file.
  9. Click OK to accept these changes and to close the Project Settings dialog box.
  10. Click the Update All Dependencies before rebuilding the project so that the compiler will use the new common header file. With Visual C++ 2.x, this option is on the Project menu; with Visual C++ 4.0, this option is on the Build menu.

Modification Type:MajorLast Reviewed:10/2/2003
Keywords:kbhowto kbide KB137515