I use a global “config.h” in my project to define various flags that enable/disable features. I defined using the convention:
#define ENABLE_FEATURE1 0 // feature is disabled
#define ENABLE_FEATURE2 1 // feature is enabled
And later in the source files I test for them:
#if ENABLE_FEATURE1
// do something
#endif
#if ENABLE_FEATURE2
// do something
#endif
However, it has happened a few times that I forgot to include “config.h” in a cpp file that tests for those flags. Then I get no error or warning from the preprocessor, and the program builds fine, but the compiler treats all the undefined flags as if they’re set to zero, so the feature is treated as disabled in that cpp file, while it’s treated as enabled in the rest of the source files.
Sometimes the consequences have not been immediately obvious even at runtime.
Insert the following
#ifin source files.Then, you may have another problem: what if I forgot to insert the above
#ifdef? 🙂In this case, you may include
config.hin a precompiled header (e.g.,stdafx.h) and turn on using precompiled header. If a file doesn’t includestdafx.h, MSVC will raise an error.Finally, you may write a simple script to check whether every source code has
config.h.