Reputation: 409
I am reading a book on linux programming, and in a header file for error handling, they show this block of code with a macro that stops 'gcc -Wall' from complaining control reaches end of non-void function. I don't completely understand how it works.
#ifdef __GNUC__
/* macro stops 'gcc -Wall' complaining that 'control reaches
end of non void function' if we use following functs to
terminate main() or some other non-void funct */
#define NORETURN __attribute__ ((__noreturn__))
#else
#define NORETURN
#endif
void errExit(const char *format, ...) NORETURN;
void err_exit(const char *format, ...) NORETURN;
void errExitEN(int errnum, const char *format, ...) NORETURN;
...
#endif
I would like to know exactly what it is doing, and how. Any help would be appreciated. Thank you.
Upvotes: 2
Views: 182
Reputation: 206577
When you use gcc
, the line
void errExit(const char *format, ...) NORETURN;
is translated to
void errExit(const char *format, ...) __attribute__ ((__noreturn__));
by the preprocessor. What does the __noreturn__
attribute to do?
A few standard library functions, such as
abort
andexit
, cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them noreturn to tell the compiler this fact.
You can read more about __noreturn__
and other function attributes supported by gcc
at http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Function-Attributes.html.
When use a different compiler, the same line is translated to:
void errExit(const char *format, ...);
Upvotes: 2