gcc的__attribute__参数

10 篇文章 0 订阅
3 篇文章 0 订阅
#define __pure			__attribute__((pure))
#define __aligned(x)		__attribute__((aligned(x)))  //x个字节对齐
#define __aligned_largest	__attribute__((aligned))  //最小的2^n个字节对齐
#define __printf(a, b)		__attribute__((format(printf, a, b)))
#define __scanf(a, b)		__attribute__((format(scanf, a, b)))
#define __maybe_unused		__attribute__((unused))
#define __always_unused		__attribute__((unused))
#define __mode(x)		__attribute__((mode(x)))
#define __malloc		__attribute__((__malloc__))
#define __used			__attribute__((__used__))
//tells the compiler that the function won't ever return
#define __noreturn		__attribute__((noreturn)) 
//取消结构在编译过程中的优化对齐(使用1字节对齐),按照实际占用字节数进行对齐
//但内部的成员变量struct 不会被“pack”
#define __packed		__attribute__((packed))  
#define __weak			__attribute__((weak))
#define __alias(symbol)		__attribute__((alias(#symbol)))
#define __cold			__attribute__((cold))
#define __section(S)		__attribute__((__section__(#S)))
#define __must_check		__attribute__((warn_unused_result))
#define notrace			__attribute__((hotpatch(0, 0)))
#define __naked			__attribute__((naked)) notrace
#define __compiler_offsetof(a, b)	__builtin_offsetof(a, b)
#define __gnu_inline	__attribute__((gnu_inline))
#define noinline	__attribute__((noinline))
#define __always_inline inline __attribute__((always_inline))
//section,就得说RO RI ZI了,在ARM编译器编译之后,代码被划分为不同的段,
// RO Section(ReadOnly)中存放代码段和常量,
// RW Section(ReadWrite)中存放可读写静态变量和全局变量,
// ZI Section(ZeroInit)是存放在RW段中初始化为0的变量
// 将作用的函数或数据放入指定名为"section_name"对应的段中。
__attribute__((section("section_name")))

eg:
aligned (alignment)

// 该属性设定一个指定大小的对齐格式(以字节 为单位),例如:

struct S {

short b[3];

} __attribute__ ((aligned (8)));

typedef int int32_t __attribute__ ((aligned (8)));

函数属性(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)));

参考:
https://www.cnblogs.com/embedded-linux/p/5801999.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值