c - 这个宏在这种情况下如何工作?

标签 c

我正在读一本关于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 and exit, 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/

相关文章:

c - 使用 "NA"将 R 数组传递给 C 函数

objective-c - 如何让 modf 或 modff 正常工作?

c++ - 是否有可以忽略函数的 gcc pragma?

c - 使用 ptrace 跟踪系统调用

c - 关于使用 size_t

c - 变量意外改变

c - 指向字符数组的指针如何表现?

c - 如何在滚动窗口布局中添加 GtkEventBox?

c - c99 模式下的 VScode "expression must have a constant value"

arrays - 使用 C 将一维字符数组复制到二维字符数组