标题两个功能是由gcc的Attributes constructor和destructor实现的。
constructor属性 :使函数在main()函数之前执行
destructor属性 :使函数在main()函数完成或调用exit()之后被执行
这两个属性都还可以指定优先级,控制使用修饰的函数的执行顺序,优先级的值必须大于100,因为0到100之间的优先级由gcc来使用,优先级的值越小,优先级越高,会优先执行。
注意:如果是接收到信号退出,例如SIGSEGV或者SIGKILL信号,destructor属性修饰的函数则不会被调用。
参考:http://blog.csdn.net/justlinux2010/article/details/11695645 写的源码,发现没怎么说明优先级,那优先级是如何使用的呢。
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
static void __attribute__((constructor)) pre_main1(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
-
-
static void __attribute__((constructor)) pre_main2(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
-
-
static void __attribute__((constructor)) pre_main3(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
-
-
int main(void)
-
{
-
printf("Exiting.....\n");
-
return 0;
-
}
-
-
static void __attribute__((destructor)) back_main1(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
-
-
static void __attribute__((destructor)) back_main2(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
执行结果:
Come to pre_main1
Come to pre_main2
Come to pre_main3
Exiting.....
Come to back_main2
Come to back_main1
看一下gcc在线文档Declaring Attributes of Functions对这两个属性的说明:
constructor
destructor
constructor (priority)
destructor (priority)
The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.
You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see ). However, at present, the order in which constructors for C++ objects with static storage duration and functions decorated with attribute constructor are invoked is unspecified. In mixed declarations, attribute init_priority can be used to impose a specific ordering.
英文部分和一开始介绍的基本相同,补充了一点是 constructor 属性函数优先级从小到大,destructor 是由大到小 顺序执行。
对例子中的代码加优先级:
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
static void __attribute__((constructor)) pre_main1(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
-
-
static void __attribute__((constructor(101))) pre_main2(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
-
-
static void __attribute__((constructor(102))) pre_main3(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
-
-
int main(void)
-
{
-
printf("Exiting.....\n");
-
return 0;
-
}
-
-
static void __attribute__((destructor(101))) back_main1(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
-
-
static void __attribute__((destructor)) back_main2(void)
-
{
-
printf("Come to %s\n", __func__);
-
}
执行结果:
Come to pre_main2
Come to pre_main3
Come to pre_main1
Exiting.....
Come to back_main2
Come to back_main1
阅读(1573) | 评论(0) | 转发(0) |