Fortunately, the __attribute__ mechanism was cleverly designed in a way to make it easy to quietly eliminate them if used on platforms other than GNU C. Superficially, __attribute__ appears to have multiple parameters (which would typically rule out using a macro), but the two sets of parentheses effectively make it a single parameter, and in practice this works very nicely.
/* If we're not using GNU C, elide __attribute__ */ #ifndef __GNUC__ # define __attribute__(x) /*NOTHING*/ #endif
Note that __attribute__ applies to function declarations, not definitions, and we're not sure why this is. So when defining a function that merits this treatment, an extra declaration must be used (in the same file):
/* function declaration */
void die(const char *format, ...) __attribute__((noreturn))
__attribute__((format(printf,1,2)));
void die(const char *format, ...)
{
/* function definition */
}
本文探讨了GCC中__attribute__机制的设计精妙之处及其在不同平台上的灵活使用方法。作者详细解释了如何通过预处理器定义使其在非GNU编译器环境中被忽略,并展示了这一机制在函数声明中的具体应用实例。
8613

被折叠的 条评论
为什么被折叠?



