__attribute__概述

__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 语言的一部分。因此,它的使用可能在不同的编译器上有所不同。在使用时,应该查阅相应的编译器文档以确保正确使用该特性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值