Sunday, May 22, 2016

The Five Stages of #undef rad1

This was sitting in my "drafts" folder from many moons ago. Looking back on it it's entertaining, so I'll post it now.

If you've had to deal with Windows in C++ land, you are probably aware of some of the many "fun" #defines that Microsoft is guilty of, such as near and far. Or, similarly frustrating for anyone programming trigonometric functions, rad1 and rad2. In the case of the latter, they sometimes manifest in the form of:
Error 1 error C2143: syntax error : missing ')' before 'constant' c:\repos\codebase\lib\headerThatUsesTrig.h
In this "article," I cover the five stages of dealing with Microsoft's rampant #defines, starting from your first completely oblique compiler error, like above.


1. Denial and Isolation

You just got the error. Humph. That looks pretty obscure. Chances are it will go away once I clean things up in this file.

2. Anger

What the hell is going on? Ugh, that senior engineer who gets away with everything probably messed up his #includes in that header, and as usual you need to clean up his mess.

Oh, that wasn't it? Guh, I probably just misunderstood one of those headers that the other senior engineer wrote. They're so obtuse. No mere mortal can possibly understand them.

The plight of the junior software engineer.
Stand strong.

3. Bargaining

Maybe if I do a clean rebuild...

Maybe if I just go back a few commits and RE-WRITE EVERY CHARACTER IN MY FILE...

Maybe if I re-install the third-party library...

Maybe if I grep for EVERY PLACE THAT HEADER IS USED...

MAYBE IF I JUST HIDE UNDER MY DESK...

4. Depression

What did you do wrong? It's got to be something stupid—something obvious. It's got to be a typo somewhere in your code. How did your code break something way over in the /lib/ folder? Oh god, you're an impostor of a programmer. You shouldn't be working here. You can't tell anyone that you did this. You'll get fired. Double check everything. Triple check everything. Put in your two weeks now. 

5. Acceptance

It's cool. Compilation failure. I've done a code inspection and my stuff looks good. I just have to accept that things aren't always what they seem. I need to remember what's going on under the hood.

Okay, what's under the hood? Well I'm compiling C++ which does preprocessor stuff before actually building—that can lead to some strange stuff since it literally changes your code before compiling. And I'm doing an embedded-system emulator which goes through Visual Studio. Let's see how to get Visual Studio's build system to spit out the preprocessor output.

Wow, that preprocessor file looks weird. "rad1" was replaced with something else. Let's try an experiment.

#undef rad1
Whoa, it built!

5b. Getting to the root of the problem so you don't put in a patch-work fix

// TODO

No comments:

Post a Comment