Thread: Header File Macros

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    11

    Header File Macros

    I am having trouble understanding how these macros I see in header files work. I looked back into my texts and understand basic macro operations but I do not understand this.

    I understand line 8: replace NORETURN with __attribute__ ((__noreturn__)).

    But line 10 just defines the word.

    Then what is the point of NORETURN at the end of a function argument list?

    This is an excerpt from Linux Programming Interface by Michael Kerrisk
    page 52. Header file is error_functions.h

    Code:
    #ifndef ERROR_FUNCTIONS_H
    #define ERROR_FUNCTIONS_H
    
    void errMsg(const char *format, ...);
    
    #ifdef __GNUC__
    
    #define NORETURN __attribute__ ((__noreturn__))
    #else
    #define NORETURN
    #endif
    
    void errExit(const char *format, ...) NORETURN;
    
    void errExit(const char *format, ...) NORETURN;
    void err_exit(const char *format, ...) NORETURN;
    ....
    void fatal(const char *format, ...) NORETURN;
    
    ...........

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you compiling with gcc - the __GNUC__ will be defined and you will get your __attribute__ ((__noreturn__)) added to each function in place of NORETURN

    if you compiling with any other compiler, where the __GNUC__ is not defined - NORETURN will be empty string and preprocessor will just remove is from the source code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Since the identifier NORETURN is used in the program, it must be defined, otherwise it's a syntax error. So in your example, if __GNUC__ is defined, then NORETURN is defined as __attribute__ ((__noreturn__)), giving
    Code:
    void errExit(const char *format, ...) __attribute__ ((__noreturn__));
    etc.
    whereas if __GNUC__ is not defined, NORETURN is defined as nothing, giving
    Code:
    void errExit(const char *format, ...) ;
    etc.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    11
    ahh . thank you guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defines, Macros and Header files
    By hnparmar in forum C Programming
    Replies: 4
    Last Post: 03-26-2013, 05:31 PM
  2. OPEN an Excel file with MACROS DISABLED
    By highlowjack in forum Windows Programming
    Replies: 6
    Last Post: 11-02-2007, 06:10 PM
  3. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. Replies: 6
    Last Post: 04-02-2002, 05:46 AM