__attribute__((constructor)) 修饰的函数在main函数之前执行
2015-07-24 17:45
525 查看
最近研究qemu,初始化的时候有类似的代码:
do_qemu_init_** 想必是模块的初始化了,但是却没有调用 do_qemu_init的地方,奇了怪了,为什么呢?
仔细看看这个函数,修饰符中包含了 __attribute__((constructor)),估计就是这个家伙干的!
写代码测试之:
编译执行:
果然是这样,
__attribute__((constructor)) 修饰的函数在main函数之前执行
__attribute__((destructor)) 修饰的函数在main函数之后执行
#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 main\n"); } void static __attribute__((destructor)) after_main() { printf("after main\n"); } 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函数之后执行
相关文章推荐
- 朴素贝叶斯算法(naive Bayes algorithm)
- Android控件使用手册:使用WebView打开http://www.baidu.com
- 邮件部署
- Linux /bin, /sbin, /usr/bin, /usr/sbin 区别 http://blog.csdn.net/xh16319/article/details/12861523
- Hdu 2015 Multi-University Training Contest2
- HDU 1151 Air Raid【最小路径覆盖】
- SGU Magic Pairs
- main函数的参数
- !codeforces 399D Painting The Wall-概率dp
- Baidu Map API 1
- gentoo Hardened/Toolchain
- [INSTALL_FAILED_OLDER_SDK]错误
- [leetcode] 219. Contains Duplicate II
- [leetcode] 217.Contains Duplicate
- ORA-29786: SIHA attribute GET failed with error
- uva 10803 Thunder Mountain (floyd)
- hdu 5291 Candy Distribution 2015 Multi-University Training Contest 1
- hdu 5291 Assignment 2015 Multi-University Training Contest 1
- 在VirtualBox中安装Ubuntu14报错:Failed to open a session for the virtual machine,Unable to load R3……
- 如何定义AIDL跨进程间通信