GCC设置函数属性为constructor和destructor
2016-03-26 18:18
447 查看
在阅读FIO源码过程中,看到引擎的注册函数fio_libaio_register与反注册函数fio_libaio_unregister都没有其他函数调用,而fio又没有以动态库的形式将这两个函数供别的地方使用。。郁闷。。再次看时发现两个函数的前面分别有宏fio_init和fio_exit,其定义为别为:
回头看看GCC的属性设置吧。。__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。__attribute__语法格式为:__attribute__
( ( attribute-list ) )
若函数被设定为constructor属性,则该函数会在main()函数执行之前被自动的执行。若函数被设定为destructor属性,则该函数会在main()函数执行之后或者exit()被调用后被自动的执行。示例如下:
#define fio_init __attribute__((constructor)) #define fio_exit __attribute__((destructor))这不正是C++中的构造函数与析构函数么。。。
回头看看GCC的属性设置吧。。__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。__attribute__语法格式为:__attribute__
( ( attribute-list ) )
若函数被设定为constructor属性,则该函数会在main()函数执行之前被自动的执行。若函数被设定为destructor属性,则该函数会在main()函数执行之后或者exit()被调用后被自动的执行。示例如下:
#include <stdio.h> #include <stdlib.h> void __attribute__((constructor)) con_func() { printf("befor main: constructor is called..\n"); } void __attribute__((destructor)) des_func() { printf("after main: destructor is called..\n"); } int main() { printf("main func..\n"); return 0; }输出:
befor main: constructor is called.. main func.. after main: destructor is called..
相关文章推荐
- JavaScript精炼之构造函数 Constructor及Constructor属性详解
- Array, Array Constructor, for in loop, typeof, instanceOf
- JavaScript constructor和instanceof,JSOO中的一对欢喜冤家
- 不用构造函数(Constructor)new关键字也能实现JavaScript的面向对象
- JavaScript中几个重要的属性(this、constructor、prototype)介绍
- 理解Javascript_11_constructor实现原理
- JavaScript中constructor()方法的使用简介
- javascript new后的constructor属性
- 深入分析js中的constructor和prototype
- php示例详解Constructor Prototype Pattern 原型模式
- Javascript的构造函数和constructor属性
- JavaScript中的prototype和constructor简明总结
- FIO测试硬盘IOPS
- linux使用FIO测试磁盘的iops
- 【JavaScript】关于原型继承
- 两个HorizontalScrollView一起滚动
- C#中Struct与Class的区别
- 自定义dialog--symbian
- 模式 工厂
- c++中的引用的使用原理和使用实例 (2)