Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347623
  • 博文数量: 94
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 606
  • 用 户 组: 普通用户
  • 注册时间: 2015-09-30 08:58
个人简介

x

文章分类

全部博文(94)

文章存档

2019年(4)

2018年(10)

2017年(26)

2016年(38)

2015年(16)

我的朋友

分类: LINUX

2017-10-12 12:39:24

__attribute__((constructor)):被修饰的函数在main函数之前调用
__attribute__((destructor)):被修饰的函数在main函数之后调用

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. static void __attribute__ ((constructor)) __reg_module(void)
  4. {
  5.     printf("__reg_module called.\n");
  6. }

  7. static void __attribute__ ((destructor)) __unreg_module(void)
  8. {
  9.     printf("__unreg_module called.\n");
  10. }

  11. int main(int argc, const char *argv[])
  12. {
  13.     printf("main called.\n");
  14.     
  15.     return 0;
  16. }
打印结果如下:

点击(此处)折叠或打开

  1. __reg_module called.
  2. main called.
  3. __unreg_module called.
注意:
#1) 可执行程序或库文件都可以使用此属性修饰函数;
#2) 同一个可执行程序或库文件允许多个函数被修饰,执行顺序由代码编写顺序或编译链接顺序有关;
#3) 全局变量(对象)的构造和析构函数分别会在__attribute__((constructor))和__attribute__((destructor))二者修饰的函数之前运行;
阅读(1913) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~