首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

用 _attribute_ 将函数登记到.ctors段 使接口在main之前执行

2012-11-26 
用 __attribute__ 将函数注册到.ctors段 使接口在main之前执行#include stdio.hvoidmy_init(){printf(H

用 __attribute__ 将函数注册到.ctors段 使接口在main之前执行

#include <stdio.h>

void  my_init()
{
     printf("Hello ");
}

typedef void (*ctor_t)(void);

ctor_t __attribute__ ((section(".ctors"))) my_init_p = my_init;

int main()
{
     printf("world \n");
     return 0;
}

输出:Hello world

 

该程序等同于

 

#include <stdio.h>

void my_init(void) __attribute__ ((constructor));

void  my_init()
{
     printf("Hello ");
}

int main()
{
     printf("world \n");
     return 0;
}

 

热点排行