standards: Conditional Compilation
1
1 3.5 Conditional Compilation
1 ===========================
1
1 When supporting configuration options already known when building your
1 program we prefer using 'if (... )' over conditional compilation, as in
1 the former case the compiler is able to perform more extensive checking
1 of all possible code paths.
1
1 For example, please write
1
1 if (HAS_FOO)
1 ...
1 else
1 ...
1
1 instead of:
1
1 #ifdef HAS_FOO
1 ...
1 #else
1 ...
1 #endif
1
1 A modern compiler such as GCC will generate exactly the same code in
1 both cases, and we have been using similar techniques with good success
1 in several projects. Of course, the former method assumes that
1 'HAS_FOO' is defined as either 0 or 1.
1
1 While this is not a silver bullet solving all portability problems,
1 and is not always appropriate, following this policy would have saved
1 GCC developers many hours, or even days, per year.
1
1 In the case of function-like macros like 'REVERSIBLE_CC_MODE' in GCC
1 which cannot be simply used in 'if (...)' statements, there is an easy
1 workaround. Simply introduce another macro 'HAS_REVERSIBLE_CC_MODE' as
1 in the following example:
1
1 #ifdef REVERSIBLE_CC_MODE
1 #define HAS_REVERSIBLE_CC_MODE 1
1 #else
1 #define HAS_REVERSIBLE_CC_MODE 0
1 #endif
1