我正在读一本关于Linux编程的书,在错误处理的头文件中,他们用一个宏来显示这段代码块,该宏阻止'gcc -Wall'提示控制到达非void函数的末尾。我不完全明白它是如何工作的。
#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
我想确切地知道它在做什么以及如何做。任何帮助,将不胜感激。谢谢。
最佳答案
当您使用gcc
时,该行
void errExit(const char *format, ...) NORETURN;
翻译为
void errExit(const char *format, ...) __attribute__ ((__noreturn__));
由预处理器处理。 __noreturn__
属性有什么作用?
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.
您可以在 http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Function-Attributes.html 阅读有关 __noreturn__
以及 gcc
支持的其他函数属性的更多信息。 .
当使用不同的编译器时,同一行被翻译为:
void errExit(const char *format, ...);
关于c - 这个宏在这种情况下如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29191529/