在main函数之前被调用的函数

//这种函数在main()之前执行。
#include <stdio.h>

/*
 * This attribute lead gcc/ld to exec this function 
 * before the "main".
 */
__attribute__ ((__constructor__)) 
  void pre_func(void) {
        printf("pre_func\n");
}

同理在main函数退出只有执行的函数
static void stop(void) attribute ((destructor));

只需要有attribute ((constructor)) 关键字修饰即可

猜你喜欢

转载自blog.csdn.net/guchuanhang/article/details/78605106