I have been debugging a particularly insidious bug which I now believe to be caused by unexpected changes which stem from different behavior when different headers are included (or not).
This is not exactly the structure of my code but let's just take a look at this scenario:
#include "Newly_created_header_which_accidentally_undefines_SOME_DEFINE.h"
// ...
#ifdef SOME_DEFINE
code_which_i_believe_i_am_always_running();
#else
code_which_fails_which_i_have_forgotten_about(); // runtime error stack traces back here, but I don't know this... or maybe it's some strange linker error
#endif
I search through my git commits and narrow down the cause of the bug, compiling and running my code countless times, only to find after several hours that the only difference required for causing the bug is the inclusion of what appears to be a completely benign and unrelated header.
Perhaps this is a great argument for why the preprocessor basically just sucks.
But I like it. The preprocessor is cool because it lets us make shortcuts. It's only that some of these shortcuts, when not used carefully, bite us in the butt pretty hard.
So at this juncture it would have helped if I could use a directive like #echo "Running old crashy code"
where I'll be able to see this during compilation so I could be tipped off immediately to start investigating why SOME_DEFINE was not defined.
As far as I know the straightforward way of determining if SOME_DEFINE is defined is to do something like
#ifndef SOME_DEFINE
printf("SOME_DEFINE not defined!!\n");
This will surely get the job done but there is no good reason for this task to be performed at runtime because it is entirely determined at compile-time. This is simply something I'd like to see at compile-time.
That being said, in this situation, using the print (or log or even throwing an exception) may be an acceptable thing to do because I won't really care about slowing down or cluttering up the questionable code. But that doesn't apply if I have for instance two code paths both of which are important, and I just want to know at compile-time which one is being activated. I'd have to worry about running the code that does the preprocessor-conditioned print at the beginning of the program.
This is really just a long-winded way of asking the question, "Can I echo a string to the output during compilation by using a preprocessor directive?"