GCC attribute 机制

    GNU C的一大特色(却不被初学者所知)就是__attribute__机制。__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。
  __attribute__书写特征是:__attribute__前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__参数。
  __attribute__语法格式为:
  __attribute__ ((attribute-list))
  其位置约束为:
  放于声明的尾部“;”之前。
  函数属性(Function Attribute)
  函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强大。__attribute__机制也很容易同非GNU应用程序做到兼容之功效。
  GNU CC需要使用 –Wall编译器来击活该功能,这是控制警告信息的一个很好的方式。下面介绍几个常见的属性参数。
  __attribute__ format
  该__attribute__属性可以给被声明的函数加上类似printf或者scanf的特征,它可以使编译器检查函数声明和函数实际调用参数之间的格式化字符串是否匹配。该功能十分有用,尤其是处理一些很难发现的bug。
  format的语法格式为:
  format (archetype, string-index, first-to-check)
   format属性告诉编译器,按照printf, scanf, strftime或strfmon的参数表格式规则对该函数的参数进行检查。“archetype”指定是哪种风格;“string-index”指定传入函数的第几个参数是格式化字符串;“first-to-check”指定从函数的第几个参数开始按上述规则进行检查。
  具体使用格式如下:
  __attribute__((format(printf,m,n)))
  __attribute__((format(scanf,m,n)))
  其中参数m与n的含义为:
  m:第几个参数为格式化字符串(format string);
  n:参数集合中的第一个,即参数“…”里的第一个参数在函数参数总数排在第几,注意,有时函数参数里还有“隐身”的呢,后面会提到;
  在使用上,__attribute__((format(printf,m,n)))是常用的,而另一种却很少见到。下面举例说明,其中myprint为自己定义的一个带有可变参数的函数,其功能类似于printf:
  //m=1;n=2
  extern void myprint(const char *format,...) __attribute__((format(printf,1,2)));
  //m=2;n=3
  extern void myprint(int l,const char *format,...) __attribute__((format(printf,2,3)));
  需要特别注意的是,如果myprint是一个函数的成员函数,那么m和n的值可有点“悬乎”了,例如:
  //m=3;n=4
  extern void myprint(int l,const char *format,...) __attribute__((format(printf,3,4)));
  其原因是,类成员函数的第一个参数实际上一个“隐身”的“this”指针。(有点C++基础的都知道点this指针,不知道你在这里还知道吗?)
  这里给出测试用例:attribute.c,代码如下:
  1:
  2:extern void myprint(const char *format,...) __attribute__((format(printf,1,2)));
  3:
  4:void test()
  5:{
  6: myprint("i=%d/n",6);
  7: myprint("i=%s/n",6);
  8: myprint("i=%s/n","abc");
  9: myprint("%s,%d,%d/n",1,2);
  10:}
  
  运行$gcc –Wall –c attribute.c attribute后,输出结果为:
  
  attribute.c: In function `test':
  attribute.c:7: warning: format argument is not a pointer (arg 2)
  attribute.c:9: warning: format argument is not a pointer (arg 2)
  attribute.c:9: warning: too few arguments for format
  
  如果在attribute.c中的函数声明去掉__attribute__((format(printf,1,2))),再重新编译,既运行$gcc –Wall –c attribute.c attribute后,则并不会输出任何警告信息。
  注意,默认情况下,编译器是能识别类似printf的“标准”库函数。
  __attribute__ noreturn
  该属性通知编译器函数从不返回值,当遇到类似函数需要返回值而却不可能运行到返回值处就已经退出来的情况,该属性可以避免出现错误信息。C库函数中的abort()和exit()的声明格式就采用了这种格式,如下所示:
  
  extern void exit(int) __attribute__((noreturn));
  extern void abort(void) __attribute__((noreturn));
  
  为了方便理解,大家可以参考如下的例子:
  
  //name: noreturn.c ;测试__attribute__((noreturn))
  extern void myexit();
  
  int test(int n)
  {
   if ( n > 0 )
   {
   myexit();
   /* 程序不可能到达这里*/
   }
   else
   return 0;
  }
  
  编译显示的输出信息为:
  
  $gcc –Wall –c noreturn.c
  noreturn.c: In function `test':
  noreturn.c:12: warning: control reaches end of non-void function
  
  警告信息也很好理解,因为你定义了一个有返回值的函数test却有可能没有返回值,程序当然不知道怎么办了!
  加上__attribute__((noreturn))则可以很好的处理类似这种问题。把
  extern void myexit();
  修改为:
  extern void myexit() __attribute__((noreturn));
  之后,编译不会再出现警告信息。
  __attribute__ const
  该属性只能用于带有数值类型参数的函数上。当重复调用带有数值参数的函数时,由于返回值是相同的,所以此时编译器可以进行优化处理,除第一次需要运算外,其它只需要返回第一次的结果就可以了,进而可以提高效率。该属性主要适用于没有静态状态(static state)和副作用的一些函数,并且返回值仅仅依赖输入的参数。
  为了说明问题,下面举个非常“糟糕”的例子,该例子将重复调用一个带有相同参数值的函数,具体如下:
  
  extern int square(int n) __attribute__((const));
  ...
   for (i = 0; i
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GCCattribute section是一种扩展机制,用于将函数或数据放入指定的段中。使用方式是在函数或数据的声明或定义前加上__attribute__((section("section_name"))),其中section_name是指定的段的名称。\[1\] 例如,可以使用attribute section将特定的变量存放在自定义的段中。可以通过以下步骤来验证这些部分: 1. 在代码中声明或定义变量,并使用attribute section将其放置在指定的段中。 2. 编译代码时,使用链接器脚本来指定段的布局。可以使用-Wl,-T选项来指定链接器脚本,例如gcc s.c -Wl,-Ts.lds。 3. 执行编译后的程序,并使用objdump命令来验证段的位置和初始值的保存。注意objdump命令的参数和自定义段中变量的位置和初始值的保存。\[2\]\[3\] 通过使用GCCattribute section,可以更灵活地管理代码和数据的布局,以满足特定的需求。 #### 引用[.reference_title] - *1* *2* [利用gcc的__attribute__编译属性section子项构建初始化函数表](https://blog.csdn.net/rdstwww/article/details/54784699)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [gcc的__attribute__((section(“”)))属性含义](https://blog.csdn.net/guanhuhousheng/article/details/76301829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值