惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
爱范儿
爱范儿
罗磊的独立博客
美团技术团队
Jina AI
Jina AI
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
IT之家
IT之家
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
月光博客
月光博客
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)

博客园 - 于光远

泛型编程 分区理论知识整理 SPI协议及驱动开发框架 I2C协议及驱动开发框架 使用qemu 加载linux-6.18.1内核 linux 内核 策略模式 单例模式 Linux性能分析:从监控到优化的完整工具链 C++模板元编程实现序列化与反序列化 引用 bootchart linux 脚本打印时间 接口编程 不定参数解析,结合tuple 类模板继承 std::ostream & operator<< Flash HIDL --HelloWorld
独立于main运行的程序
于光远 · 2022-03-10 · via 博客园 - 于光远
#include <iostream>
using namespace std;
#define INITIALIZER(f) \
    static void f(void) __attribute__((constructor)); \
    static void f(void)
#define DEINITIALIZER(f) \
    static void f(void) __attribute__((destructor)); \
    static void f(void)



static void IniFunc(){
    int a = 1+1; 
    printf("func()---1+1= %d\n",a);
    printf("func()---can not use std::cout \n");
    //cout<<"func()"<<endl;
    
}
static void deFunc(){
    cout<<"~func()"<<endl;
    int a = 1+2; 
    printf("1+2= %d\n",a);
    std::cout<<"std::cout"<<std::endl;
}
INITIALIZER(ff){
    IniFunc();
};

DEINITIALIZER(dd){
    deFunc();
    std::cout<<"can use cout"<<std::endl;
};

/*
static void f1(void) __attribute__((constructor)); 

void f1(void){
     printf("before main1111\n");
}
*/
/*
void static __attribute__((constructor)) before_main()
{
    printf("before main\n");
}
 
void static __attribute__((destructor)) after_main()
{
    printf("after main\n");

}
*/

int main(){

cout<< "in main"<<endl;
return 0;
}
ygy@ygy-VirtualBox:~/work/tmp$ g++ d.cpp 
ygy@ygy-VirtualBox:~/work/tmp$ ./a.out 
func()---1+1= 2
func()---can not use std::cout 
in main
~func()
1+2= 3
std::cout
can use cout

posted @ 2022-03-10 18:56  于光远  阅读(67)  评论()    收藏  举报