Wilcox Development Solutions Blog

How I mastered preprocessor directives (finally)

October 09, 2003

Merging C++ is not fun. After you deal with CVS issues (like merging and dealing with conflicts), you get to compile. Or try to anyway.

The first challenge was precompiling the precompiled headers. (These speed up compile time - the files are only compiled once, instead of once per #include.)

In compiling these headers, I ran across a problem - someone didn’t end an #if where they should have. This may have been a human somewhere, or just the cvs merge process not knowing any better. Here the story begins… The C++ preprocessor is great - sometimes. You can condition code out, telling the compiler to not even try. Great, especially with cross-platform apps or libraries.

Except when something goes wrong with the preprocessor. Like a missing #endif. Then it’s a bugger.

First, there are no tools to help you to debug this sort of thing. I got turned around so many times trying to find the missing #endif that I was going batty. Followed false trails several times, then had to work backwards, painstakingly backwards, when I realized that the error probably wasn’t there. At this point I had spent most of Wednesday on the problem. (Monday afternoon was spent using cvs to import/merge a vendor branch, while Tuesday was spent fighting with cvs keywords and conflicts and such.)

I finally got an idea today (Thursday). I moved half of my header files (those files that had been merged by the ever helpful cvs) out of the folder, and replaced them with ones straight from the wxWindows tree. Hmm… error still there… move half of the remaining files out, replace those with wxWindows ones. Hmm… error still there… moved half of those…

Using this technique, I was able to find the problem in probably 30-45 minutes. It was a file I would have never suspected, because I was 90% certain that the cvs merge hadn’t touched that file. Well, it did - taking an #endif with it.

What a pain anyway.


Written by Ryan Wilcox Chief Developer, Wilcox Development Solutions... and other things