一、 gcc下利用attribute关键字声明constructor和destructor的C语言
#include <stdio.h>
__attribute((constructor)) void before_main()
{
printf("%s/n", __FUNCTION__);
}
__attribute((destructor)) void after_main()
{
printf("%s/n", __FUNCTION__);
}
int main(int argc, char** argv)
{
printf("%s/n", __FUNCTION__);
return 0;
}
二、C++利用全局变量和构造函数特性
#include <Iostream>
using namespace std;
class TestClass
{
public:
TestClass();
};
TestClass::TestClass()
{
cout<<"TestClass"<<endl;
}
TestClass Ts;
int main()
{
cout<<"main"<<endl;
return 0;
}