当前位置:网站首页>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
边栏推荐
- Detailed discussion on data synchronization tools ETL, ELT, reverse ETL
- Problems in SSM project configuration, various dependencies, etc. (for personal use)
- json.dumps()函数解析
- 【activiti】流程实例
- 自己的一点点小想法——使用MATLAB实现类似环形缓冲器的读取
- [activiti] activiti system table description
- plsql查询数据乱码
- "Statistical learning methods (2nd Edition)" Li Hang Chapter 15 singular value decomposition SVD mind map notes and after-school exercise answers (detailed steps) SVD matrix singular value Chapter 15
- ThreadLocal stores the current login user information
- 统计信号处理小作业——瑞利分布噪声中确定性直流信号的检测
猜你喜欢

What do programmers often mean by API? What are the API types?

The way to attack the first poca hackson project "Manta network"

Multi merchant mall system function disassembly Lecture 11 - platform side commodity column

PDF文本合并

学习率余弦退火衰减之后的loss

多商户商城系统功能拆解06讲-平台端商家入驻协议

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

在网络中添加SE通道注意力模块

Similarities and differences of ODS, data mart and data warehouse

Multi merchant mall system function disassembly lecture 07 - platform side commodity management
随机推荐
Watermelon book / Pumpkin book -- Chapter 1 and 2 Summary
Connect CRM system and effect advertising, help enterprises with precision marketing, and help enterprises with precision marketing
【activiti】activiti流程引擎配置类
Authorized access to MySQL database
labelme转voc代码中的一个小问题
Multi merchant mall system function disassembly Lecture 11 - platform side commodity column
Too many database connections
比较好的CV链接收藏(动态更新)
"Statistical learning methods (2nd Edition)" Li Hang Chapter 14 clustering method mind map notes and after-school exercise answers (detailed steps) K-means hierarchical clustering Chapter 14
统计信号处理小作业——瑞利分布噪声中确定性直流信号的检测
Add se channel attention module to the network
ThreadLocal stores the current login user information
对ArrayList<ArrayList<Double>>排序
likeshop单商户SAAS商城系统无限多开
Recommend a fully open source, feature rich, beautiful interface mall system
【activiti】activiti系统表说明
opencv读取avi视频报错:number < max_number in function ‘icvExtractPattern
Jupyter notebook选择conda环境
ThreadLocal存储当前登录用户信息
数据归一化