GNU C/C++ __attributes__

来源:互联网 发布:今天怎么登录不上淘宝 编辑:程序博客网 时间:2024/06/06 02:42
使用__attribute__修饰函数和变量的属性方面的代码,做个总结:

GCC使用__attribute__关键字来描述函数,变量和数据类型的属性,用于编译器对源代码的优化。
描述函数属性的几个重要的关键字:

  • void noreturnfun() __attribute__((noreturn));//函数不会返回。
  • void centon()__attribute__((alias("__centon")));//设置函数别名,函数是__cencon,别名是centon.
  • void main_enter()__attribute__((constructor));//main_enter函数在进入main函数前调用
  • void main_exit()__attribute__((destructor));//main_exit函数在main函数返回后调用
  • void fun() __attribute__((noinline));//fun函数不能作为inline函数优化
  • void fun() __attribute__((section("specials”)));//将函数放到specials段中,而不是通常的text段中
  • no_instrument_function、constructor和destructor关键字主要用于剖析(profiling)源代码的。
  • __attribute__(format(archetype,string-index,first-to-check)):format attribute提供了依照printf, scanf, strftime,strfmon类型函数的参数格式对目标函数进行类型的检查.
  • __attribute__((weak)): weak symbol,弱符号. 若存在两个相同的全局符号时,会引发重定义错误.如果使用weak attribute,则当weak symbol和non-weaksymbol同时存在的时候,linker会使用non-weak symbol.若只有weak symbol存在的时候则只使用weaksymbol.
  • __attribute__((deprecated)): deprecated,弃用.如果在源文件在任何地方地方使用deprecated attribute函数,编译器将会发出警告.
  • __attribute__((aligned(ALIGNMENT))):指定变量或结构体最小字节对齐数,以byte为单位.ALIGNMENT:指定的字节对齐操作数. 
  • __attribute__((cleanup(cleanup_function)):当一个变量的作用域消失时,便会执行后面的clean_function函数.
  • __attribute__((packed)):使变量或者是结构体按照最小的对齐方式,对于变量是1byte对齐,对于字段,也就是field指bit对齐. 
上面只是对常见的一些属性操作的解释,对于其他的用法应当参照GCC提供的文档。
0 0
原创粉丝点击