DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

(standards.info.gz) Conditional Compilation

Info Catalog (standards.info.gz) Standard C (standards.info.gz) Design Advice
 
 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.
 
    While this is not a silver bullet solving all portability problems,
 following this policy would have saved the GCC project alone many person
 hours if not 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
 
Info Catalog (standards.info.gz) Standard C (standards.info.gz) Design Advice
automatically generated byinfo2html