前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言__attribute__ ((constructor))和__attribute__ ((destructor))

C语言__attribute__ ((constructor))和__attribute__ ((destructor))

原创
作者头像
IT工作者
发布2022-05-20 18:19:45
1.6K0
发布2022-05-20 18:19:45
举报

一、gcc为函数提供了几种类型的属性,其中包含:构造函数(constructors)和析构函数(destructors),可带优先级。

       使用类似下面的方式来指定这些属性:

       static

       void start(void) __attribute__ ((constructor));

       static

       void stop(void) __attribute__ ((destructor));

二、带有"构造函数"属性的函数将在main()函数之前被执行,而声明为"析构函数"属性的函数则将在main()退出时执行。

三、C语言测试代码。

#include <stdio.h>  
__attribute__((constructor)) void load_file()
{
    printf("Constructor is called.\n");
}
 
__attribute__((constructor(100))) void load_file1()
{
    printf("Constructor 100 is called.\n");
}
 
__attribute__((constructor(102))) void load_file2()
{
    printf("Constructor 102 is called.\n");
}
 
__attribute__((constructor(99))) void load_file3()
{
    printf("Constructor 99 is called.\n");
}
 
 
__attribute__((destructor)) void unload_file()
{
    printf("destructor is called.\n");
}
  
int main(int argc, char **argv)  
{  
    printf("this is function %s\n", __func__);  
    return 0;  
}  

四、adb push 编译出来的bin文件到android设备上运行

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档