Same precompiled header for multiple projects in the same solution RRS feed

  • Question

  • Hello!

    We got a large VS2010 solution 56 C++ projects, 36 of these create a static library, one create a final .exe, and 19 small projects that link one (or two) of the static libs to create a test .exe.

    All of the library-creating projects (also the final .exe one) create and use a precompiled header, and all of these headers contain the same stuff: STL headers, Boost headers, and a few library headers we use. The results for all projects are a ~36MB .pch file, and it takes about 5s to build. For all the 37 projects, the PCHs are 1.3 GIGABYTES in total, and takes 3 min to build them only. I suppose the linker is also slowed down by the extra 1.3 GB input.

    So I have tried to edit the solution so that we have only one .pch file for the whole solution: Created one project containing only the stdafx.h, set it to create a PCH file, and set all projects to use the created PCH file. Trying to compile it, I got errors the compiler not finding the pch file. Turned out there is a build step defined in the Microsoft.Cpp.Win32.targets file to delete the PCH if program database file not exists:


      <Delete Condition="..stuff.. and !Exists(%(ClCompile.ProgramDataBaseFileName))"
          Files="%(ClCompile.PrecompiledHeaderOutputFile)" />


    Oh well of course the pdb does not exist, I am just trying to compile the very first file of the second project.. So essentially MSBuild first deletes the pch file then complains it not exists.. DUH.

    The question is: How can I use the same PCH file for multiple projects, what settings should be taken care of?

    Wednesday, August 4, 2010 10:59 AM


  • Hello Lrdx,

    Yes, you can share same PCH file for multiple projects. To do it, please refer to following steps:

    1. Build one project which include precompiled header firstly as master project ;

    2. Delete all stdafx.h file in other projects;

    3. Set Additional Include Directories ( Project Properties > Configuartion Properties > C/C++ > General)  to the folder of the master project so the compiler can find the master stdafx.h file;

    4. Set Precompiled Header Output File ( C/C++ > Precompiled Headers ) to master project's file;

    5. Make other projects depend on master project so it always get built first. To do it, you can right click project node ,and choose Project Dependencies.

    For more information, please refer to Nobugz's answer in following thread:


    Nancy Shao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Nancy Shao Wednesday, August 11, 2010 6:01 AM
    Thursday, August 5, 2010 3:24 AM