使用GCC扩展attribute,在main函数外进行操作
来源:互联网 发布:andriod连接mac电脑 编辑:程序博客网 时间:2024/05/17 04:13
以前知道这个GCC扩展特性,但是一直没有试过。今天写了个小的测试程序试验一下。
- #include <stdlib.h>
- #include <stdio.h>
- static void __attribute__ ((constructor)) my_init()
- {
- printf("my_init\n");
- }
- static void __attribute__ ((destructor)) my_fini()
- {
- printf("my_fini\n");
- }
- static void exit_clean()
- {
- printf("exit_clean\n");
- }
- int main()
- {
- atexit(exit_clean);
- printf("Enter main\n");
- return 0;
- }
执行结果为:
- my_init
- Enter main
- exit_clean
- my_fini
可见,使用__attribute__((constructor))可以使函数在main之前被执行,而__attribute((destructor))可以在main函数执行完毕后执行。其实这两个扩展特性,主要是GCC为C++的全局类的构造和析构函数准备的。但是对于C来说,在特定的情况下,我们同样也可以使用这两个扩展特性,来完成一些特殊的需求。
0 0
- 使用GCC扩展attribute,在main函数外进行操作
- gcc main函数
- gcc main函数
- pbfunc外部函数扩展应用-在Powerbuilder中进行Http的GET、POST操作
- 【Unity编辑器扩展简介】在MonoBehaviour中可使用的编辑器扩展Attribute
- gcc扩展,在Linux Kernel中的使用示例
- gcc扩展,在kernel中的使用(怎么)
- 在main函数之前和之后执行一些操作
- C++中如何在main()函数之前执行操作?
- C++中如何在main()函数之前执行操作?
- C++中如何在main()函数之前执行操作?
- C++中如何在main()函数之前执行操作?
- 使用gcc的-finstrument-functions选项进行函数跟踪
- 使用gcc的-finstrument-functions选项进行函数跟踪
- gcc对main函数的作用
- 使用DataGrid中扩展ItemRenderer和HeaderRenderer进行操作
- 使用PHP的soap扩展, 进行wdsl操作
- gcc 原子操作函数
- objective-c算法详解(一、链表)
- 写程序不仅仅是写出来完事
- 线程局部变量ThreadLocal
- JSP内置对象(一)
- VirtualBox中eth0不存在解决方法
- 使用GCC扩展attribute,在main函数外进行操作
- A Simple Review Of 《Query Languages for Graph Databases》(III)
- HTML/CSS IE6、7兼容性问题、bug总汇
- ZOJ 3430 Detect the Virus (AC自动机)
- 进制转换/码制转换和计算机大端和小端模式
- 梯度下降(批量&随机)和非负矩阵分解
- Git之2——基本操作
- 在eclipse 中运行 需sudo root权限执行的程序
- jquery 实现可编辑div