-
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;
...........
-
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
-
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.
-