您的位置:首页 > 大数据 > 人工智能

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

2012-11-25 22:49 399 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: