CPP Info Memo Part 5 -- Diagnostics

| 分类 C-Family  | 标签 preprocessor  diagnostics 

#error and #warning are used for diagnostics.

  • #error will cause proprocessor to report a fatal error and stop pre-processing.

    #error normally should be placed inside a conditional that detects a combination of parameters which should not be supported, for example:

    #ifdef __vax__
    #error "Won't work on VAXen.  See comments at get_last_object."
    #endif
    

    Or:

    #if !defined(UNALIGNED_INT_ASM_OP) && defined(DWARF2_DEBUGGING_INFO)
    #error "DWARF2_DEBUGGING_INFO requires UNALIGNED_INT_ASM_OP."
    #endif
    
  • #warning is like #error, but cuases the preprocessor to issue a wanrning and then continue preprocessing. It is usually used in obsolete header files:
    #ifndef _OBSOLETE_H_
    #define _OBSOLETE_H_
    
    #warning "This file is obsoleted..."
    
    #endif /* _OBSOLETE_H_ */
    

Neither #error nor #warning macro-expands its argument. Internal whitespace sequences are each replaced with a single space. The line must consist of complete tokens. It is wisest to make the argument of these directives be a single string constant; this avoids problems with apostrophes and the like.


上一篇     下一篇