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