__attribute__
2012-05-08 00:00
162 查看
今天和同事讨论babeltrace的时候,一直找不到新format库的入口,后来仔细查找了一下已有的格式转换ctf-text的注册函数定义:void __attribute__((constructor)) ctf_text_init(void),发现了这个有趣的使用方法: __attribute__((constructor))这个东东类似于C++的构造函数,在main()前被调用. 类似的还有__attribute__((destructor))。 下面是一个网上搜索到的一个测试例子:
__attribute__((constructor))
__attribute__((destructor))
#include<stdio.h>
__attribute__((constructor)) void before_main()
{
printf("before main\n");
}
__attribute__((destructor)) void after_main()
{
printf("after main\n");
}
int main()
{
printf("in main\n");
return 0;
}
$ gcc test.c -o test
$ ./test
before main
in main
after main
根据上面的代码以及输出结果,我们可以猜到__attribute__((constructor))表示这段代码将在main函数前调用,就像在C++里面的全局变量类的构造一样.
说到C++里面的全局类对象的构造,我们不禁要问全局类对象的构造跟__attribute__((constructor))以及destructor谁在前谁在后呢?
#include<iostream>
using namespace std;
__attribute__((constructor)) void before_main()
{
cout<<"Before Main"<<endl;
}
__attribute__((destructor)) void after_main()
{
cout<<"After Main"<<endl;
}
class AAA{
public:
AAA(){
cout<<"AAA construct"<<endl;
}
~AAA(){
cout<<"AAA destructor" <<endl;
}
};
AAA A;
int main()
{
cout<<"in main"<<endl;
return 0;
}
$ make test2
$ ./test2
AAA construct
Before Main
in main
AAA destructor
After Main
可以看到全局类的构造过程发生在before_main()函数前面,而析构也发生在after_main()前面.
__attribute__((constructor))
__attribute__((destructor))
#include<stdio.h>
__attribute__((constructor)) void before_main()
{
printf("before main\n");
}
__attribute__((destructor)) void after_main()
{
printf("after main\n");
}
int main()
{
printf("in main\n");
return 0;
}
$ gcc test.c -o test
$ ./test
before main
in main
after main
根据上面的代码以及输出结果,我们可以猜到__attribute__((constructor))表示这段代码将在main函数前调用,就像在C++里面的全局变量类的构造一样.
说到C++里面的全局类对象的构造,我们不禁要问全局类对象的构造跟__attribute__((constructor))以及destructor谁在前谁在后呢?
#include<iostream>
using namespace std;
__attribute__((constructor)) void before_main()
{
cout<<"Before Main"<<endl;
}
__attribute__((destructor)) void after_main()
{
cout<<"After Main"<<endl;
}
class AAA{
public:
AAA(){
cout<<"AAA construct"<<endl;
}
~AAA(){
cout<<"AAA destructor" <<endl;
}
};
AAA A;
int main()
{
cout<<"in main"<<endl;
return 0;
}
$ make test2
$ ./test2
AAA construct
Before Main
in main
AAA destructor
After Main
可以看到全局类的构造过程发生在before_main()函数前面,而析构也发生在after_main()前面.
相关文章推荐
- 用JS查看修改CSS样式(cssText,attribute('style'),currentStyle,getComputedStyle)
- Android Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法
- addFieldToFilter 与 addAttributeToFilter使用总结
- No resource identifier found for attribute 'showAsAction'
- 函数属性__attribute__((constructor))和__attribute__((destructor))
- 多个类定义attr属性重复的问题:Attribute "xxx" has already been defined
- [Spring MVC] - @ModelAttribute使用
- addAttributeToFilter Conditionals In Magento
- 【MVC类库学习笔记】编写第一个MVC类-Attribute
- jQuery中[attribute!=value]选择器用法实例
- No resource identifier found for attribute 'parentActivityName' in package 'android'
- SpringMVC常用注解實例詳解2:@ModelAttribute GOOD
- android:onClick attribute
- Attribute之基础
- GCC扩展 __attribute__ ((visibility("hidden")))
- GCC的__attribute__ ((constructor))和__attribute__ ((destructor))
- 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__
- solution:AttributeError: 'module' object has no attribute 'PY2'
- jsp中request.getParameter和request.getAttribute之间的区别
- __attribute__((packed))结构体的成员地址顺序及大小问题