attribute的用法

本文介绍了C语言中__attribute__((constructor))属性的使用,展示了如何通过这个属性注册在main函数执行前运行的初始化函数,例如设置信号处理等预处理操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

attribute的用法

#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <ucontext.h>
//写几个优先级函数
static __attribute__((constructor(101))) void before1();
static __attribute__((constructor(102))) void before2();
static __attribute__((constructor(103))) void before3();
static __attribute__((constructor(104))) void before4();
int main()
{   
     printf("main.c\n");
}
static __attribute__((constructor(101))) void before1()
{
    printf("before1\n");
}
static __attribute__((constructor(102))) void before2()
{
    printf("before2\n");
}
static __attribute__((constructor(103))) void before3()
{
    printf("before3\n");
}
static __attribute__((constructor(104))) void before4()
{
    printf("before4\n");
}

执行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s5Vul19C-1616938931536)(C:\Users\11066\AppData\Roaming\Typora\typora-user-images\1616938832808.png)]

最直观的效果就是可以注册一些在main入口之前的一些操作

931536)]

最直观的效果就是可以注册一些在main入口之前的一些操作

比如注册一些信号用于监听段错误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值