#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;
}
本文介绍了一种使用C语言实现构造函数属性的方法,并通过具体示例展示了如何定义和使用构造函数来初始化程序。这种方法可以确保某些初始化操作在main函数执行前完成。
789

被折叠的 条评论
为什么被折叠?



