gcc __attribute__ 引言

8 篇文章 0 订阅

      本文只针对gcc 对c语言编译 的一些选项进行讨论。

      每个编译器都有自身的一些特性,gcc也不例外,当然要使用这些选项会导致代码的可移植性变差,当然这些新特性可以让编译器更好的了解你代码的意图,编译出更优秀的机器码,我把这些C标准之外的特性当作工具使用。__attribute__ 就是利器之一。

      以下摘自Using the GNU Compiler Collection   For gcc version 4.4.7 文档,我们先看看gcc最权威的文档如何介绍__attribute__的用途

      5.27 Declaring Attributes of Functions
In GNU C, you declare certain things about functions called in your program which help
the compiler optimize function calls and check your code more carefully.
The keyword __attribute__ allows you to specify special attributes when making
a declaration. This keyword is followed by an attribute specification inside double
parentheses. The following attributes are currently defined for functions on all targets:
aligned, alloc_size, noreturn, returns_twice, noinline, always_inline, flatten,
pure, const, nothrow, sentinel, format, format_arg, no_instrument_function,
section, constructor, destructor, used, unused, deprecated, weak, malloc,
alias, warn_unused_result, nonnull, gnu_inline, externally_visible, hot, cold,
artificial, error and warning. Several other attributes are defined for functions
on particular target systems. Other attributes, including section are supported for
variables declarations (see Section 5.33 [Variable Attributes], page 303) and for types (see
Section 5.34 [Type Attributes], page 311).

      5.27 (章节) 声明函数的属性

       在GNU C中,你声明被你的程序调用的函数的某些事情有助于编译器优化函数调用和对代码更小心的检测。

关键字__attribute__允许你在声明的时候指定特殊的属性。关键字属性规定在双括号内。下面的关键字被

用于定义在所有的目标上:

aligned, alloc_size, noreturn, returns_twice, noinline, always_inline, flatten,
pure, const, nothrow, sentinel, format, format_arg, no_instrument_function,
section, constructor, destructor, used, unused, deprecated, weak, malloc,
alias, warn_unused_result, nonnull, gnu_inline, externally_visible, hot, cold,
artificial, error and warning。

 在特殊的目标系统上还有几个其他用于函数的属性。其他的属性,包含变量的属性(5.33章节 303页)和类型属性

(5.34章节 311页)


5.33 Specifying Attributes of Variables
The keyword __attribute__ allows you to specify special attributes of variables or structure
fields. This keyword is followed by an attribute specification inside double parentheses.
Some attributes are currently defined generically for variables. Other attributes are defined

for variables on particular target systems. Other attributes are available for functions

(see Section 5.27 [Function Attributes], page 278) and for types (see Section 5.34 [Type
Attributes], page 311). Other front ends might define more attributes (see Chapter 6
[Extensions to the C++ Language], page 529).

      5.33 (章节) 描述变量的属性

关键字__attribute__ 允许你去指定变量或者结构体域的特殊属性。关键字属性规定在双括号内。

一些通用属性被定义于变量。其他属性被应用与特殊的目标系统。其他函数属性(5.27章节 278页)

和类型属性(5.34章节  311页),其他前段可能定义更多的属性(第6章  C++ 扩展 529页)


5.34 Specifying Attributes of Types
The keyword __attribute__ allows you to specify special attributes of struct and union
types when you define such types. This keyword is followed by an attribute specification inside
double parentheses. Seven attributes are currently defined for types: aligned, packed,
transparent_union, unused, deprecated, visibility, and may_alias. Other attributes
are defined for functions (see Section 5.27 [Function Attributes], page 278) and for variables
(see Section 5.33 [Variable Attributes], page 303).

    5.34 (章节) 描述类型属性

 关键字__attribute__允许你去描述你定义的结构体类型或者联合类型的特殊属性。关键字属性规

定在双括号内。目前有7个属性:

aligned, packed,transparent_union, unused, deprecated, visibility, and may_alias. 

其他属性,被定义于函数(5.27章节 278页)和变量属性(5.33章节 303页)


翻译若有不正之处,欢迎大家指正。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值