One of the best (but little known) features of GNU C is the __attribute__ mechanism, which allows a developer to attach characteristics to function declarations to allow the compiler to perform more error checking. It was designed in a way to be compatible with non-GNU implementations, and we've been using this for years in highly portable code with very good results.
Table of Contents
- __attribute__ format
- __attribute__ noreturn
- __attribute__ const
- Putting them together
- Compatibility with non-GNU compilers
- Other References
Note that __attribute__ spelled with two underscores before and two after, and there are always two sets of parentheses surrounding the contents. There is a good reason for this - see below. Gnu CC needs to use the -Wall compiler directive to enable this (yes, there is a finer degree of warnings control available, but we are very big fans of max warnings anyway).
GCC属性机制详解
本文介绍了GNU C Compiler (GCC)中鲜为人知但极其强大的__attribute__机制。此特性允许开发者为函数声明附加特性,帮助编译器进行更严格的错误检查。文章详细探讨了几种常用的__attribute__应用案例,并讨论了其在非GNU编译器上的兼容性。
1025

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



