To make a coustructor in C language.
//ctor.c
#include <stdio.h>
void my_init() {
printf("Hello");
}
typedef void (*ctor_t)();
ctor_t __attribute__((section (".ctors"))) my_init_p = &my_init;
int main() {
printf("World!/n");
return 0;
}
//cont.c
#include <stdio.h>
void __attribute__ ((constructor)) my_init();
void my_init() {
printf("Hello ");
}
int main() {
printf("World!/n");
return 0;
}
June 8th Monday (六月 八日 月曜日)
最新推荐文章于 2025-08-21 09:06:15 发布
本文提供了两个使用C语言实现构造函数的例子。第一个例子通过使用属性指定符将构造函数置于特定段中,第二个例子则利用了编译器的构造函数属性来自动调用初始化函数。
1632

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



