none
Shared precompiled header

    Question

  • Is there a supported way to use shared precompiled header in VC 2008 SP1?
    Monday, November 10, 2008 5:20 PM

Answers

  • Yeah, no problem.  Designate one project as the "master", to be built first.  For each project, delete stdafx.h.  Project + properties, C/C++, General.  Set Additional Include Directories to the folder of the master project so the compiler can find the master stdafx.h file.  C++, Precompiled Header File, set to the master's .pch file.  Project + Project dependencies, make it depend on the master so it always get built first.
    Hans Passant.
    • Marked as answer by jack 321 Friday, November 14, 2008 2:40 AM
    Tuesday, November 11, 2008 1:13 AM
    Moderator

All replies

  • Shared between...?
    MSMVP VC++
    Monday, November 10, 2008 8:52 PM
  •  Multiple projects.
    Monday, November 10, 2008 10:02 PM
  • Yeah, no problem.  Designate one project as the "master", to be built first.  For each project, delete stdafx.h.  Project + properties, C/C++, General.  Set Additional Include Directories to the folder of the master project so the compiler can find the master stdafx.h file.  C++, Precompiled Header File, set to the master's .pch file.  Project + Project dependencies, make it depend on the master so it always get built first.
    Hans Passant.
    • Marked as answer by jack 321 Friday, November 14, 2008 2:40 AM
    Tuesday, November 11, 2008 1:13 AM
    Moderator
  • What happened when you tried?
    Hans Passant.
    Tuesday, November 11, 2008 4:33 PM
    Moderator
  • I have not yet. We have 100+ projects, so I need some kind of certainty before trying.
    Tuesday, November 11, 2008 4:44 PM
  • Well, you could try 2 of those projects.  For guarantees, you should probably call Microsoft CSS.  Please post back when you find out.
    Hans Passant.
    • Proposed as answer by bdwade100 Thursday, December 16, 2010 5:57 PM
    Tuesday, November 11, 2008 5:35 PM
    Moderator
  • Hi Alex,

    We are maintaining this kind of configuration for our projects since VS 2003 (we are now using VS 2008).

    The reason is that we are providing a framework of C++ classes, with a bunch of header files that our clients include in their own developments (their solutions are often made of > 100 projects). Our clients  share a precompiled header project in their VC++ solution to avoid to compile our header files for every project, since this is really time consuming.

    The problems described in the link above are real, so we had to find workarounds since suggestion #98646 was postponed:

    - Parallel building results in read-locks of the shared pch file.

     Our clients have to disable parallel building

    - Error with debug builds: error C2859: (...)\vc90.pdb is not the pdb file that was used when this precompiled header was created, recreate the precompiled header.

    For this one we had to find a trick : the "Program Database file Name" C++ option of our projects are set to the file name of our precompiled header project ($(IntDir)\mypchprojetname). As a pre-build-step, we copy the .pdb and .idb file from our pch project folder output to the current project folder output:

    xcopy /D /Y "$(SolutionDir)\mypchprojetname\$(IntDir)\mypchprojetname.pdb" ".\$(IntDir)".

    xcopy /D /Y "$(SolutionDir)\mypchprojetname\$(IntDir)\mypchprojetname.idb" ".\$(IntDir)".

     The reason we do this is because the Program Database File is modified during the build process to include debug info for the compiled files and then can not be shared between projects.

    Notice the /D option that will only copy the file when it's newer. If you don't set this option, the project pdb file will be replaced by the one from the pch project when you just link your project, and the debug info from your project sources will be crashed, resulting in the following warning for every source files: myfile.obj : warning LNK4204: '(...)\mypchprojetname.pdb' is missing debugging information for referencing module; linking object as if no debug info.

    Hope this will help.

     

    • Proposed as answer by bdwade100 Thursday, December 16, 2010 5:51 PM
    Friday, January 23, 2009 2:16 PM
  • Reginald's answer worked for me--thanks!
    Thursday, December 16, 2010 5:52 PM
  • I have done it in Visual Studio 2010. It seems to be working with two notes:

    1) there are no .idb files

    2) Compiler issues Warning #670: precompiled header file "xxxx.pchi" was not generated in this directory

    Should Reginald's procedure be amended for VS2010?

     

    Friday, February 11, 2011 12:35 AM
  • Reginald's answer was very helpful for using third party pch files across several projects.  But it's only a partial fix in my situation.  I have a base project that builds a static lib/pch.  I then use this lib/pch pair as middleware for applications.  I link the lib in using config properties->linker->input->additional dependencies.  I use the pch via the /Yu flag to use the pch.  The search paths are consistent with both projects. 

    The problem is I don't see objects (much less inheritance) in the locals view, or auto view.  All I see are anonomous blue diamonds that represent classes.  This is consistent with objects defined in the library, as well as objects defined in the application.  There are no warnings or notifications during build, or debug. 

    This apparently means that there's a disconnect between what the debugger expects and what results in the pdb file.  But I don't understand enough about visual studio to understand what's going in the background. 

    I tried removing one complexity; generating/using pch.  The problem went away!  But I had to wait 10x for the lilb to build.  I would much prefer to use the pch, but not if it costs me visibility in debug. 

    Anyone care to provide insight? 


    UPDATE:  After turning off pch generatation, I then turning pch generation back on, but tried to build with /Z7 option.  This took even longer to build, and actually ran out of memory before it could complete.  I then reverted all modifications, built again, then launched the application that uses the lib/pch pair.  The problem dissapeared. 

    I couldn't tell you what produced this flaky behavior.  But, this ended up resolving the issue.  As much as I hate not understand ingwhat caused the problem; I'm not going to spend a day figuring it out.  For those that experience this same issue, you might try following the same path though. 

     

    • Edited by doc taco Thursday, April 07, 2011 10:54 PM issue resolved
    Thursday, April 07, 2011 9:52 PM
  • So it seems that pre-compiled headers shared between multiple projects are not supported by the Visual Studio compilers and IDE. Only by using nasty tricks one may happen to get it work one way or another. Am I right?

    I am suffering from the same problem. We do not use .sln and .vcproj files at all. We use make instead because of the same code is compiled on 6 very different platforms and Windows is only one of them. It would be great if cl.exe and link.exe would be able to use .pch files (and the shared .pdb part of symbol database for that puprose) when executed in multiple instances in parallel. I would be able to live with little symbol information missing, but it is hard to get over the error:

    fatal error C1033: cannot open program database '......\vc90.pdb'

    I would really like to know why is symbol information from multiple files being stored into a single file (while each .c and .cpp files have their own .pdb). Or if that is not the case, why is the .pdb being locked? Or what is going on there?

    This eror does not happen when I run the compilation with disabled parallel builds. Since I use 8 core machine this means practically 8 times slower builds (around 5 times in practice; 5min ->25min).

    Does anybody know what is the current situation in Visual Studio 2008, 2010 and 2012? Should I start working on some hand crafted workaround or is this issue fixed in some release?

    With thanks and regards

    Martin Dobsik

    Monday, September 24, 2012 11:06 AM
  • PLEASE VOTE FOR THIS: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/4931119-allow-precompiled-headers-to-be-shared-between-pro
    Friday, November 08, 2013 9:30 AM