The Science of Debugging
Chapter 7


7.1 Known Tricks, Problems, and Pratfalls with MSVC

7.1.1 Precompiled Headers

Precompiled headers are exactly what they sound like; headers which have already been compiled. To save time, Microsoft threw in a feature which allowed the common headers (such as MFC libraries) to be compiled but once, and all that time is saved! They tend to work very well in such situations, except for a small two headed problem.

So, the first thing we're going to cover is the (hopefully) rare instance in which you have added files to your project and you cannot compile thanks to a friendly, "fatal error C1010: unexpected end of file while looking for precompiled header directive.".

That's shorthand for "Microsoft assumes everyone in the world conforms to their standards, why do you have to be so different?" Why use of precompiled headers is an assumed default, I'll never know.

For everyday use of a CircleMUD, you're going to want to disable that bad boy. So, go to Project Settings, and in the 'Settings For' listbox, select 'All Configurations'.

Then go to the 'C/C++ tab', and select 'Precompiled Headers'. Now, select all the folders in the project tree box on the left hand side, and finally click on the 'Not Using Precompiled Headers' radio button.

I know, that's a lot. It'll look like this:

Now, some of you goombas may want to use precompiled headers, and also be hit with venomous snakes and have firecrackers go off in your nose. That's fine, I'm not one to question your self-destructive lifestyle.

First, get to the project settings window you see above in the 'how to turn precompiled headers off' description. Now, expand the folder(s) in the window to the left and expand the resultant source code tree(s). Select all of the source files, those with a .c or .cpp extension, ( click and use the control key unless you like setting it one at a time) and select the 'Use precompiled header file (.pch)' radio button. Fill in the blank with a header file name. The standard name is 'Stdafx.h', based on the MFC's original design name, "Application Framework". Apparently "FullOfCrap.h" voided the old 8 by 3 filename length issue.

This header file will now be responsible for creating the precompiled header that the other source files will use. In CircleMUD, that means you'll want to put common headers like 'conf.h', 'sysdep.h', and the sort in there, erase those includes in all the files using precompiled headers, and add a new #include pointed at the newly created header file. What a pain huh?

Remember, all you get from precompiled headers is a speedier compile, and headaches.

If you want to play with the 'create a pch' radio button, MSVC will save the 'state' of that file, and any code it contains (including other #include'ed headers) as a precompiled header. It sometimes works in pretty much the same way, though use of magic pragma directives will allow you to get it to function in a nearly identical manner. It doesn't seem worth the effort to me though.

Make sure that nothing, ever, EVER is set to 'Automatic use of precompiled headers'. If you do, something will break. I don't know what, but you should never let MSVC guess what it is you're trying to accomplish. You will be burned.

Also, if you're creating, using, or otherwise abusing precompiled headers, note that the options (in most patchlevels of MSVC 6 at least) are not very 'sticky'. What I mean by this is that you may set the options and radio buttons, click on apply, and click 'ok' to close it - and those options don't stay at what you set them to. Alternatively, you may set those options, they stick, and they act as if they are not set. It's an MSVC bug, and here's the work around - click back and forth between each file you're setting in the project tree to the left, and back to the option you're setting, after you're done setting it the first time.

I lied. Proper use of precompiled headers can help you avoid redefinition of constants when included by multiple files and other similar problems. I still hate them.



Index
7. Before You Start Debugging with MSVC (Visual Studio) 7.1.2 Color