8

I use Visual C++ 2012 with a project that makes a heavy use of precompiled headers. So heavy that the infamous /Zm switch is in use.

When I cancel a build in progress, I sometimes get this error on the next build:

error C1852: 'foo.pch' is not a valid precompiled header file

Nine times out of ten, things will go smoothly, but when this happens I have to find the .pch and delete it manually before restarting the build.

That annoys me a bit. Is there a way to prevent this from happening? A patch from Microsoft? Or a way to force Visual to delete the .pch and restart the build automatically when the issue occurs? Or some other solution I didn't think about?

EDIT: Here's the version of Visual I'm running:

Microsoft Visual Studio Professional 2012
Version 11.0.61030.00 Update 4
Laurent Couvidou
  • 32,354
  • 3
  • 30
  • 46
  • I removed the C++ tag as this is a compiler-specific question not a language question. – Mark B Jan 14 '14 at 15:47
  • 5
    @MarkB I disagree, there are probably more people, who are also familiar with VC, that monitor the C++ tag than there are who monitor those compiler specific tags. Rolled back to rev 1. – Praetorian Jan 14 '14 at 16:04
  • 1
    Doesn't a Rebuild kill the pch file? If I get any pch error, I just hit Rebuild. – Roger Rowland Jan 14 '14 at 16:12
  • You ask if MS have provided a patch, but you don't tell us which version you're actually running. November 12, 2013 saw [Update 4](http://www.visualstudio.com/en-us/news/news-overview-vs) - are you running with that? – icabod Jan 14 '14 at 16:39
  • You could create a pre build event that deletes all `.pch` files. – bolov Jan 14 '14 at 18:35
  • @RogerRowland It does indeed, but it's a huge project so a fuild rebuild takes way more time than a simple delete of the guilty .pch + a normal build. – Laurent Couvidou Jan 15 '14 at 08:06
  • @icabod Indeed, I should I've specified the version I'm running. Yes I'm running Update 4, see my edit. – Laurent Couvidou Jan 15 '14 at 08:07
  • @bolov Sure, but isn't it roughly an equivalent of doing a full rebuild every time? As said above, the project is huge so that would mean losing a lot of time. – Laurent Couvidou Jan 15 '14 at 08:11
  • Ok, it may be you can write an extension to do this, I did look to see if you can detect a build cancel as a Build Event so you could add a Custom Build Event to delete the pch, but it seems there is no such hook. – Roger Rowland Jan 15 '14 at 08:11
  • @RogerRowland Ha, that was a good idea, too bad it doesn't exist. Thank you for having a look! – Laurent Couvidou Jan 15 '14 at 08:14
  • @Praetorian: We do not pick question tags based on the number of followers of those tags. We pick them based on how appropriate they are for the question. (Spamming the popular tags with everything even only loosely related is in fact _harmful_ - a tag with a million questions in it per hour becomes quite useless.) That being said, I don't necessarily disagree with your decision in this instance. – Lightness Races in Orbit Jan 15 '14 at 10:24
  • @LightnessRacesinOrbit Of course I'm not advocating tagging questions with popular tags to increase views, otherwise I'd have also tagged it C# or something. Strictly speaking, MarkB is correct that this is not related to the language itself, but tagging it as such does increase its chances of getting answered quite a bit. And being a question about C++ compiler settings, it's related and not an abuse of tags. And you agree with all of this. Anyway, it's my fault for posting something on SO that leaves open the slightest possibility for misinterpretation :-P – Praetorian Jan 15 '14 at 17:13

3 Answers3

1

This is a pure conjecture, as I did not run into this issue.

Try to find out how Visual detect a .pch file is corrupted (i.e. empty file, file not correctly ended, ...). If it follow a clear pattern, write a pre-build script that parse all .pch and delete corrupted ones.

rockeye
  • 2,765
  • 2
  • 30
  • 44
  • No no there might be a pattern, maybe the file is just plain empty after all or has a suspiciously small size. I'll look into that. Good idea! – Laurent Couvidou Jan 15 '14 at 13:33
1

I would create a script that would attempt to recompile the stdafx.cpp file, but this time using the PCH instead of generating it. I.e. the expected outcome is the successful compilation of an empty file. If this fails, delete the PCH. Now run this script as a pre-build step.

It sounds fairly expensive, but it's very reliable. Any problem loading the PCH causes its regeneration, even compiler upgrades. Also, your PCH is now in file cache, which means the actual use is slightly cheaper.

This might be implemented as an NMAKE build script with somewhat unusual rules.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • This sounds like a good workaround if I can't find any pattern, I just have to see how expensive would be that .pch pre-checking. – Laurent Couvidou Jan 15 '14 at 13:36
  • It could be fairly fast if done via NMAKE, when you create a small .OK file if the PCH file passes this test. As long as that .OK file is newer than the .PCH, you don't need to recheck the .PCH – MSalters Jan 15 '14 at 14:01
0

I followed rockeye's suggestion of trying to find a pattern in these corrupted files. Turns out it's very simple: valid files start with a VCPCH0 header, corrupted files don't.

A simple C# program run as a Pre-Build Event of the failing project(s) and deleting the corrupted files solves the issue. If anyone's interested, the source is right here.

Laurent Couvidou
  • 32,354
  • 3
  • 30
  • 46