www.delorie.com/gnu/docs/GNU/standards_11.html   search  
 
Buy GNU books!


GNU Coding Standards

[ < ] [ > ]   [ << ] [ 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.

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


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003