当前位置:网站首页>GCC 中__attribute__((constructor)和__attribute__(((destructor))的注意事项。
GCC 中__attribute__((constructor)和__attribute__(((destructor))的注意事项。
2022-07-24 05:20:00 【喵喵锤锤你小可爱】
属性__attribute__((constructor)和__attribute__(((destructor))
- __attribute__是可以修饰多个函数的,这给当然不例外。
- 但是需要注意的是执行顺序,同一属性的函数执行顺序似乎不太好确定。原因的话等什么时候看看手册吧。
- 如果是编译C++的话,我在WSL上用gcc(
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04))编译发现无法运行,但是用Win10上装的(gcc version 4.9.2 (i686-posix-sjlj, built by strawberryperl.com project))又可以运行。至于是全局类的构造过程、析构过程、attribute((constructor)和__attribute__(((destructor))发生的顺序,无法确定。
#include <stdio.h>
__attribute__((constructor)) void before_main1()
{
printf("before main1\n");
}
__attribute__((constructor)) void before_main2()
{
printf("before main2\n");
}
__attribute__((destructor)) void after_main2()
{
printf("after main2\n");
}
__attribute__((destructor)) void after_main1()
{
printf("after main1\n");
}
int main()
{
printf("in main\n");
printf("before_main1: %p\n", before_main1);
printf("before_main2: %p\n", before_main2);
printf("after_main1: %p\n", after_main1);
printf("after_main2: %p\n", after_main2);
return 0;
}
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
before main1
before main2
in main
before_main1: 0x7fe4e40b4169
before_main2: 0x7fe4e40b4180
after_main1: 0x7fe4e40b41ae
after_main2: 0x7fe4e40b4197
after main1
after main2
TDM-GCC 4.9.2 64-bit Release/Debug
before main2
before main1
in main
before_main1: 0000000000401530
before_main2: 000000000040154B
after_main1: 0000000000401581
after_main2: 0000000000401566
after main2
after main1
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
before main1
before main2
in main
before_main1: 0x40057d
before_main2: 0x40058d
after_main1: 0x4005ad
after_main2: 0x40059d
after main1
after main2
#include<iostream>
using namespace std;
__attribute__((constructor)) void before_main1()
{
cout<<"Before Main1"<<endl;
}
__attribute__((constructor)) void before_main2()
{
cout<<"Before Main2"<<endl;
}
__attribute__((destructor)) void after_main1()
{
cout<<"After Main1"<<endl;
}
class AAA{
public:
AAA(){
cout<<"AAA construct"<<endl;
}
~AAA(){
cout<<"AAA destructor" <<endl;
}
};
AAA A;
__attribute__((destructor)) void after_main2()
{
cout<<"After Main2"<<endl;
}
int main()
{
cout<<"in main"<<endl;
return 0;
}
* .\a.exe
AAA construct
Before Main2
Before Main1
in main
After Main1
After Main2
AAA destructor
#include<iostream>
using namespace std;
__attribute__((constructor)) void before_main1()
{
cout<<"Before Main1"<<endl;
}
__attribute__((constructor)) void before_main2()
{
cout<<"Before Main2"<<endl;
}
__attribute__((destructor)) void after_main1()
{
cout<<"After Main1"<<endl;
}
__attribute__((destructor)) void after_main2()
{
cout<<"After Main2"<<endl;
}
class AAA{
public:
AAA(){
cout<<"AAA construct"<<endl;
}
~AAA(){
cout<<"AAA destructor" <<endl;
}
};
AAA A;
int main()
{
cout<<"in main"<<endl;
return 0;
}
gcc version 4.9.2 (i686-posix-sjlj, built by strawberryperl.com project)
* .\a.exe
AAA construct
Before Main2
Before Main1
in main
After Main1
After Main2
AAA destructor
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
Segmentation fault
边栏推荐
- Recommend a fully open source, feature rich, beautiful interface mall system
- Are you still trying to limit MySQL paging?
- PyTorch 单机多卡分布式训练
- Xshell远程访问工具
- 多商户商城系统功能拆解05讲-平台端商家主营类目
- PLSQL query data garbled
- 《机器学习》(周志华)第2章 模型选择与评估 笔记 学习心得
- The way to attack the first poca hackson project "Manta network"
- 关于卷积神经网络中的“输入通道”和“输出通道”的概念
- 《信号与系统》(吴京)部分课后习题答案与解析
猜你喜欢

Multi merchant mall system function disassembly lecture 13 - platform side member management

jestson安装ibus输入法

The SaaS mall system of likeshop single merchant is built, and the code is open source without encryption.

推荐一款完全开源,功能丰富,界面精美的商城系统

多商户商城系统功能拆解03讲-平台端商家管理

【activiti】activiti入门

Canal+kafka actual combat (monitor MySQL binlog to realize data synchronization)

《信号与系统》(吴京)部分课后习题答案与解析

《统计学习方法(第2版)》李航 第十三章 无监督学习概论 思维导图笔记

多商户商城系统功能拆解05讲-平台端商家主营类目
随机推荐
Inventory of well-known source code mall systems at home and abroad
Zotero快速上手指南
删除分类网络预训练权重的的head部分的权重以及修改权重名称
Chapter 5 neural network
Erp+rpa opens up the enterprise information island, and the enterprise benefits are doubled
多商户商城系统功能拆解08讲-平台端商品分类
【activiti】activiti环境配置
《机器学习》(周志华) 第5章 神经网络 学习心得 笔记
Logical structure of Oracle Database
详谈数据同步工具ETL、ELT,反向ETL
对ArrayList<ArrayList<Double>>排序
第五章神经网络
Xshell远程访问工具
对接CRM系统和效果类广告,助力企业精准营销助力企业精准营销
[activiti] activiti process engine configuration class
Multi merchant mall system function disassembly lecture 08 - platform end commodity classification
DeepSort 总结
jupyter notebook一直自动重启(The kernel appears to have died. It will restart automatically.)
Add se channel attention module to the network
The method of using bat command to quickly create system restore point