How to deal with precompiled headers randomly becoming corrupted on a cancelled build?
Asked Answered
B

3

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
Bonheur answered 14/1, 2014 at 15:6 Comment(12)
I removed the C++ tag as this is a compiler-specific question not a language question.Myronmyrrh
@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.Davila
Doesn't a Rebuild kill the pch file? If I get any pch error, I just hit Rebuild.Oreopithecus
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 - are you running with that?Endure
You could create a pre build event that deletes all .pch files.Califate
@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.Bonheur
@Endure Indeed, I should I've specified the version I'm running. Yes I'm running Update 4, see my edit.Bonheur
@Califate 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.Bonheur
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.Oreopithecus
@RogerRowland Ha, that was a good idea, too bad it doesn't exist. Thank you for having a look!Bonheur
@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.Hydride
@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 :-PDavila
B
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.

Bonheur answered 15/10, 2014 at 17:12 Comment(0)
D
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.

Duong answered 15/1, 2014 at 10:22 Comment(1)
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!Bonheur
Z
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.

Zitella answered 15/1, 2014 at 10:37 Comment(2)
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.Bonheur
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 .PCHZitella
B
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.

Bonheur answered 15/10, 2014 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.