__attribute__((constructor))
The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.
构造函数属性使函数在执行进入main()之前自动被调用。同样,析构函数属性使函数在main()完成或调用exit()之后自动被调用。具有这些属性的函数对于初始化在程序执行期间隐式使用的数据很有用。
就是指在函数上方加上__attribute__((constructor))可以让这个函数在main函数执行前运行
#include<stdio.h>
__attribute__((constructor))
void print(){
printf("This is print func!\n");
return ;
}
int main(){
printf("This is main func!\n");
return 0;
}
输出
可以看到print函数在main函数之前运行了
__attribute__((constructor))可以提前初始化一些在main函数中用到的东西
如果你了解AVL树,可以看一下其代码中NIL节点的初始化例子
https://blog.csdn.net/moX980/article/details/107561605