#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
原文链接: https://www.cnblogs.com/yuguangyuan/p/15990842.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/397605
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!