C __attribute__

背景:头文件中经常会看到#define xxx__attribute__xxx,__attribute__的作用是什么呢?


__attribute__:用来设置属性的,包括函数、变量和类型。

格式:__attribute__((attribute-list)),例:__attribute__ ((aligned (4))) 、__attrubte__ ((packed))

常见参数

  • aligned:用来手动设置字节对齐。
    #define ALIGN_4 __attribute((aligned(4)))
    
    struct TEST{
        int a;
        short b;
        bool c;
    }ALIGN_4;
  • packed:取消编译优化对齐,按照实际占用字节对齐。
    #define PACKED __attribute((__packed__))
    
    struct TEST{
        int a;
        short b;
        bool c;
    }PACKED;
  • at:绝对定位,把变量或函数绝对定位到Flash中,或者定位到RAM
    const u16 gFlashDefValue[128] __attribute__((at(0x0800F000))) = {0x1234,0x1234};//定位在flash中,其他flash补充为00
    const u16 gflashdata__attribute__((at(0x0800F000))) = 0xFFFF;
    u8 USART2_RX_BUF[USART2_REC_LEN] __attribute__ ((at(0X20001000)));//接收缓冲,最大USART_REC_LEN个字节,起始地址为0X20001000.
  • section:将函数或数据放入指定名为“name”的段中。
    long long rw[10] __attribute__ ((section ("RW")));

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雲烟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值