首先在GNU gcc的官网文档中查看__attribute__
的有关资料:
属性说明符用于描述函数,变量,类型(比如结构体,联合和枚举)标签,枚举器,语句的属性。编译器可以根据这些指定属性做出相应的优化。
属性说明符的形式为 __attribute__ ((attribute-list))
。 属性列表是一个可能为空的以逗号分隔的属性序列,其中每个属性是以下之一:
- 空,会被编译器忽略
#include <stdio.h>
__attribute__(()) int main(void)
{
printf("hello world\n");
}
- 一种属性名称,比如unused,来告诉编译器它所修饰的函数或者变量是无用的,可以优化掉。但是如果对应的函数或者变量实际上是被使用的话,编译器会保留它们
#include <stdio.h>
__attribute__((unused)) int empty_fun(void)
{
return 0;
}
int main(void)
{
printf("hello world\n");
}
- 一个属性名称,后跟一个带括号的属性参数列表
比如aligned (alignment)
属性告诉编译器和链接器为它所修饰的变量的起始地址或者函数的第一条指令的地址按照要求进行对齐
#include <stdio.h>
///< The address of this variable: nums will be 4096 bytes aligned
int nums[128] __attribute__((aligned(4096)));
int main(void)
{
printf("hello world: %p\n", nums);
}