Compatibility with non-GNU compilers

来源:互联网 发布:在淘宝上怎么退货退款 编辑:程序博客网 时间:2024/05/16 13:57

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 thetwo 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, notdefinitions, 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):

/* <em>function declaration</em> */void die(const char *format, ...) <strong>__attribute__((noreturn))                                  __attribute__((format(printf,1,2)))</strong>;void die(const char *format, ...){/* <em>function definition</em> */}
0 0
原创粉丝点击