Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66079
  • 博文数量: 7
  • 博客积分: 347
  • 博客等级: 二等列兵
  • 技术积分: 127
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-26 17:18
个人简介

一句话介绍是什么?

文章分类

全部博文(7)

分类: C/C++

2011-01-28 07:58:22

在开始之前请允许我声明我们在此所用的是GNU C,使用了GCC的扩张功能,所以请不要在VC 等编译器编译失败时对它发脾气。。。
好吧上代码:
  1. /*
  2.  * 文件名:before_and_after_main.c
  3.  * 注意:使用GCC系列编译器进行编译
  4.  */

  5. #include <stdio.h>

  6. __attribute__((constructor))
  7. void   before (void);

  8. __attribute__((destructor))
  9. void   after (void);

  10. int    main
  11. (void) {
  12.         
  13.        puts ("In main\n");

  14.        return 0;
  15. }

  16. __attribute__((constructor))
  17. void   before
  18. (void) {
  19.         
  20.        puts ("Before main\n");

  21.        return;
  22. }

  23. __attribute__((destructor))
  24. void   after
  25. (void) {
  26.                 
  27.        puts ("After main\n");

  28.        return;
  29. }
来个效果:
speller@SHELL-LAB:~/code/c$ gcc -o before_and_after_main before_and_after_main.c
speller@SHELL-LAB:~/code/c$ ./before_and_after_main
Before main

In main

After main

speller@SHELL-LAB:~/code/c$

阅读(3326) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

caoxudong8182011-03-06 16:45:15

shell_way: 不懂CPP 的灰过,唯一的知识就是《C专家编程》后面的一点资料。。。.....
《C专家编程》,很好的书。

shell_way2011-03-04 23:47:41

caoxudong818: 这个特性好玩,函数的构造和析构函数。.....
不懂CPP 的灰过,唯一的知识就是《C专家编程》后面的一点资料。。。

caoxudong8182011-02-12 12:04:37

这个特性好玩,函数的构造和析构函数。