[C.C++] C语言__attribute__的使用_c attribute

858 0
Honkers 2025-3-16 23:29:03 来自手机 | 显示全部楼层 |阅读模式

收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。


如果你需要这些资料,可以戳这里获取

需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

需要注意的是,attribute 属性的效力与你的连接器也有关,如果你的连接器最大只支持16 字节对齐,那么你此时定义32 字节对齐也是无济于事的。

2、packed

使用该属性对struct 或者union 类型进行定义,设定其类型的每一个变量的内存约束。就是告诉编译器取消结构在编译过程中的优化对齐(使用1字节对齐),按照实际占用字节数进行对齐,是GCC特有的语法。这个功能是跟操作系统没关系,跟编译器有关,gcc编译器不是紧凑模式的,我在windows下,用vc的编译器也不是紧凑的,用tc的编译器就是紧凑的。

下面的例子中,packed_struct 类型的变量数组中的值将会紧紧的靠在一起,但内部的成员变量s 不会被“pack” ,如果希望内部的成员变量也被packed 的话,unpacked-struct 也需要使用packed 进行相应的约束。

  1. struct unpacked_struct
  2. {
  3. char c;
  4. int i;
  5. };
  6. struct packed_struct
  7. {
  8. char c;
  9. int i;
  10. struct unpacked_struct s;
  11. }__attribute__ ((__packed__));
复制代码

如:

  1. 在TC下:struct my{ char ch; int a;} sizeof(int)=2;sizeof(my)=3;(紧凑模式)
  2. 在GCC下:struct my{ char ch; int a;} sizeof(int)=4;sizeof(my)=8;(非紧凑模式)
  3. 在GCC下:struct my{ char ch; int a;}__attrubte__ ((packed)) sizeof(int)=4;sizeof(my)=5
复制代码

下面的例子中使用__attribute__ 属性定义了一些结构体及其变量,并给出了输出结果和对结果的分析。代码为:

  1. struct p
  2. {
  3. int a;
  4. char b;
  5. short c;
  6. }__attribute__((aligned(4))) pp;
  7. struct m
  8. {
  9. char a;
  10. int b;
  11. short c;
  12. }__attribute__((aligned(4))) mm;
  13. struct o
  14. {
  15. int a;
  16. char b;
  17. short c;
  18. }oo;
  19. struct x
  20. {
  21. int a;
  22. char b;
  23. struct p px;
  24. short c;
  25. }__attribute__((aligned(8))) xx;
  26. int main()
  27. {
  28. printf("sizeof(int)=%d,sizeof(short)=%d.sizeof(char)=%d\n",sizeof(int)
  29. ,sizeof(short),sizeof(char));
  30. printf("pp=%d,mm=%d \n", sizeof(pp),sizeof(mm));
  31. printf("oo=%d,xx=%d \n", sizeof(oo),sizeof(xx));
  32. return 0;
  33. }
复制代码

输出结果:

sizeof(int)=4,sizeof(short)=2.sizeof(char)=1

pp=8,mm=12

oo=8,xx=24

分析:都是字节对齐的原理,可以去看这儿:字节对齐

3、at

绝对定位,可以把变量或函数绝对定位到Flash中,或者定位到RAM。

1)、定位到flash中,一般用于固化的信息,如出厂设置的参数,上位机配置的参数,ID卡的ID号,flash标记等等

  1. const u16 gFlashDefValue[512] __attribute__((at(0x0800F000))) = {0x1111,0x1111,0x1111,0x0111,0x0111,0x0111};//定位在flash中,其他flash补充为00
  2. const u16 gflashdata__attribute__((at(0x0800F000))) = 0xFFFF;
复制代码

2)、定位到RAM中,一般用于数据量比较大的缓存,如串口的接收缓存,再就是某个位置的特定变量

  1. u8 USART2_RX_BUF[USART2_REC_LEN] __attribute__ ((at(0X20001000)));//接收缓冲,最大USART_REC_LEN个字节,起始地址为0X20001000.
复制代码

注意:

1)、绝对定位不能在函数中定义,局部变量是定义在栈区的,栈区由MDK自动分配、释放,不能定义为绝对地址,只能放在函数外定义。

2)、定义的长度不能超过栈或Flash的大小,否则,造成栈、Flash溢出。

4、section

提到section,就得说RO RI ZI了,在ARM编译器编译之后,代码被划分为不同的段,RO Section(ReadOnly)中存放代码段和常量,RW Section(ReadWrite)中存放可读写静态变量和全局变量,ZI Section(ZeroInit)是存放在RW段中初始化为0的变量。
        于是本文的大体意思就清晰了,__attribute__((section(“section_name”))),其作用是将作用的函数或数据放入指定名为"section_name"对应的段中。

1)、编译时为变量指定段:

  1. __attribute__((section("name")))
  2. RealView Compilation Tools for µVision Compiler Reference Guide Version 4.0
  3. Home > Compiler-specific Features > Variable attributes > __attribute__((section("name")))
  4. 4.5.6. __attribute__((section("name")))
  5. Normally, the ARM compiler places the objects it generates in sections like data and bss. However, you might require additional data sections or you might want a variable to appear in a special section, for example, to map to special hardware. The section attribute specifies that a variable must be placed in a particular data section. If you use the section attribute, read-only variables are placed in RO data sections, read-write variables are placed in RW data sections unless you use the zero_init attribute. In this case, the variable is placed in a ZI section.
  6. Note
  7. This variable attribute is a GNU compiler extension supported by the ARM compiler.
  8. Example
  9. /* in RO section */
  10. const int descriptor[3] __attribute__ ((section ("descr"))) = { 1,2,3 };
  11. /* in RW section */
  12. long long rw[10] __attribute__ ((section ("RW")));
  13. /* in ZI section *
  14. long long altstack[10] __attribute__ ((section ("STACK"), zero_init));/
复制代码

2)、编译时为函数指定段

  1. __attribute__((section("name")))
  2. RealView Compilation Tools for µVision Compiler Reference Guide Version 4.0
  3. Home > Compiler-specific Features > Function attributes > __attribute__((section("name")))
  4. 4.3.13. __attribute__((section("name")))
  5. **收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。**
  6. ![img](https://img-blog.csdnimg.cn/img_convert/04951b1c17e8f633289f2fe58ad19702.png)
  7. ![img](https://img-blog.csdnimg.cn/img_convert/a3b2c0e3d66b9364365ce5cb4b430456.png)
  8. **[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618679757)**
  9. **需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)**
  10. **一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人**
  11. **都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  12. 以戳这里获取](https://bbs.csdn.net/topics/618679757)**
  13. **需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)**
  14. **一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人**
  15. **都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4010
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行