#pragma init and #pragma fini using gcc compiler on linux

最后都变了- 提交于 2019-12-04 01:49:21

pragmas are almost all compiler-specific. GCC doesn't implement init, but you can get the same effect using the constructor function attribute:

static __attribute__((constructor)) void my_init()
{  
  //do-something
}

There's also a corresponding destructor attribute.

Apparently #pragma init and #pragma fini are only supported by GCC for Solaris:

Instead, use C++:

// init.cpp
namespace // an anonymous namespace
{
     class autoinit
     {
         public:
             ~autoinit(){ /* destruction code, if applicable */ }
         private:
             autoinit(){ /* content of myinit */ }
             static autoinit _instance;
     };

     autoinit 
     autoinit::_instance; // static instance forces static construction
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!