EDIT: Sorry for the confusion. Let me rephrase what I meant to ask: Given the code below, where should I put the #define DEBUG_ENABLE 1 so that I can toggle debug messages before compiling?
It seems the preprocessor only performs the macro substitution #define DEBUG_ENABLE 1 locally for the file. The #define in Logger.cpp and Main.cpp don't disable or enable the debug switch in Logger.h.
I've tried this in Visual Studio and an Eclipse-based IDE where the project properties allow you to #define symbols and give them a value -- but what's the point of this feature if the only way to enable the switch is to #define it from its header?
If you #define a macro to some value just before using it, there isn't much you can do elsewhere to override that value, be it in the file that include that header or in the compiler options. Now matter what you set it to, that #define is going to override the previous value.
By the way, it's standard behavior for compilers to define _DEBUG if and only if the build is for debug, and to define NDEBUG otherwise.
If you #define a macro to some value just before using it, there isn't much you can do elsewhere to override that value, be it in the file that include that header or in the compiler options. Now matter what you set it to, that #define is going to override the previous value.
I think I provided poor sample code to go with the question. I understand that the preprocessor runs exactly once through a source file to process the preprocessor directives.
Ah .. I just realized: in Main.cpp Line #1 and in Logger.cpp Line #1, I'd #included "logger.h" before I'd #define DEBUG_ENABLE . This means the resolution of the conditional in Logger.h Line #2 preceded the definition of DEBUG_ENABLE and goes to the #else case every time.
That would explain why Logger.h Line #1 is the only place where #define does its job: it's guaranteed to run before the condition is checked, no matter when #include "Logger.h" is processed (i.e., whether in Logger.cpp or in Main.cpp).
What I need then, is to ensure that #define DEBUG_ENABLE 1 happens before those #includes.
whether DEBUG_ENABLE resolves to 1 or 0 depends on whether the preprocessor processed Main.cpp or Logger.cpp first (the header guard makes the definition idempotent).
So the only way to be sure that DEBUG_ENABLE is set to the value we want is to #define it in Logger.h.
I guess I need a final "ok" from a preprocessor guru and I can mark this case closed.
You normally want to set those kinds of configuration macros in the compiler settings for the project. You wouldn't want to set DEBUG_ENABLE to 0 on one translation unit and to 1 on another by accident.