__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。__attribute__前后都有两个下划线.
__attribute__语法格式为 __attribute__ ( ( attribute-list ) )
$
若函数被设定为constructor属性,则该函数会在main()函数执行之前被自动的执行。类似的,若函数被设定为destructor属性,则该函数会在main()函数执行之后或者exit()被调用后被自动的执行。
关键字__attribute__允许你声明时指定特殊的属性。这个关键字后面跟着双层括号引起来的属性说明。目前定义用于函数的属性:
aligned
alloc_size
alloc_align
assume_aligned
noreturn
returns_twice
noinline
noclone
no_icf
always_inline
flatten
pure
const
nothrow
sentinel
format
format_arg
no_instrument_function
no_split_stack
section
constructor
destructor
used
unused
deprecated
weak
malloc
alias
ifunc
warn_unused_result
nonnull
returns_nonnull
gnu_inline
externally_visible
hot
cold
artificial
no_sanitize_address
no_address_safety_analysis
no_sanitize_thread
no_sanitize_undefined
no_reorder
bnd_legacy
bnd_instrument
stack_protect
error
warning
Other attributes, including section are supported for variables declarations, labels and for types.
You may also specify attributes with ‘__’ preceding and following each keyword. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use noreturn instead of noreturn.