最近研究qemu,初始化的时候有类似的代码:
#define module_init(function, type)
static void __attribute__((constructor)) do_qemu_init_ ## function(void) {
register_module_init(function, type);
}
do_qemu_init_** 想必是模块的初始化了,但是却没有调用 do_qemu_init的地方,奇了怪了,为什么呢?
仔细看看这个函数,修饰符中包含了 __attribute__((constructor)),估计就是这个家伙干的!
写代码测试之:
#include <stdio.h>
#include <stdlib.h>
void static __attribute__((constructor)) before_main()
{
printf("before mainn");
}
void static __attribute__((destructor)) after_main()
{
printf("after mainn");
}
int main(int argc, char** argv)
{
printf("hello world!n");
}
root@mothership:/home/source/test# gcc a.c -o a
root@mothership:/home/source/test# ./a
before main
hello world!
after main
root@mothership:/home/source/test#
果然是这样,
__attribute__((constructor)) 修饰的函数在main函数之前执行
__attribute__((destructor)) 修饰的函数在main函数之后执行
最后
以上就是丰富画板最近收集整理的关于__attribute__((constructor)) 修饰的函数在main函数之前执行的全部内容,更多相关__attribute__((constructor))内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复