浅看C语言的__attribute__关键字
attribute
GNU C编译器增加了一个__attribute__ 关键字用来声明一个函数、变量或类型的特殊属性。
申明这些属性主要用途就是指导编译程序进行特定方面的优化或代码检查。
#include <stdio.h>
//将before_main修饰为constructor属性,确保此函数在main函数之前执行
__attribute__((constructor)) void before_main()
{
printf("%s\n",__FUNCTION__);
}
//将before_main修饰为destructor属性,确保main函数退出或者调用了exit()之后,调用该函数
__attribute__((destructor)) void after_main()
{
printf("%s\n",__FUNCTION__);
}
int main( int argc, char ** argv )
{
printf("%s\n",__FUNCTION__);
return 0;
}
运行结果