__attribute__ 关键字允许指定变量、类型和函数的属性
该关键字的格式如下所示
__attribute__((attribute1, attribute2, ...))
__attribute__((__attribute1__, __attribute2__, ...))
例如
void * Function_Attributes_malloc_0(int b) __attribute__((malloc));
static int b __attribute__((__unused__));
下表总结了函数的属性设置
函数属性 等价表示
__attribute__((alias))
__attribute__((always_inline)) __forceinline
__attribute__((const)) __pure
__attribute__((constructor[(priority)]))
__attribute__((deprecated))
__attribute__((destructor[(priority)]))
__attribute__((format_arg(string-index)))
__attribute__((malloc))
__attribute__((noinline)) __declspec(noinline)
__attribute__((nomerge))
__attribute__((nonnull))
__attribute__((noreturn)) __declspec(noreturn))
__attribute__((notailcall))
__attribute__((nothrow)) __declspec(nothrow))
__attribute__((pcs("calling_convention")))
__attribute__((pure))
__attribute__((section("name")))
__attribute__((unused))
__attribute__((used)) -
__attribute__((visibility("visibility_type")))
__attribute__((weak)) __weak
__attribute__((weakref("target")))
函数的属性可以在声明的时候指定也可以在定义的时候指定,示例如下
void AddGlobals(void) __attribute__((always_inline));
__attribute__((always_inline)) void AddGlobals(void)
{
...
}
当函数的属性冲突的时候,编译器会选择更加安全或健壮的一个例如
__attribute__((used)) 比 __attribute__((unused)) 安全
__attribute__((noinline)) 比 __attribute__((always_inline)) 安全
参考连接:https://gcc.gnu.org/onlinedocs/gcc-10.5.0/gcc.pdf
注意,
__attribute__((fallthrough))是特定于编译器的语法,不是标准 C 语言的一部分。因此,它的使用可能在不同的编译器上有所不同。在使用时,应该查阅相应的编译器文档以确保正确使用该特性。
6万+

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



