广州网站建设提供商,网站开发 实战,做搜狐网站页面,洛阳网站公司哪家好DllMain可以在dll加载到进程、线程时调用#xff0c;可以做些初始化、清理的工作 但在linux上没有专门的函数#xff0c;可以使用gcc扩张属性__attribute__((constructor)) and __attribute__((destructor))来实现 类似于全局类变量#xff0c;其构造函数及析构函数会在加载…DllMain可以在dll加载到进程、线程时调用可以做些初始化、清理的工作 但在linux上没有专门的函数可以使用gcc扩张属性__attribute__((constructor)) and __attribute__((destructor))来实现 类似于全局类变量其构造函数及析构函数会在加载时自动调用。 上述方法不能实现线程attach、detach但对一般程序足够了 void __attribute__ ((constructor)) my_load(void);
void __attribute__ ((destructor)) my_unload(void);// Called when the library is loaded and before dlopen() returns
void my_load(void)
{// Add initialization code…
}// Called when the library is unloaded and before dlclose()
// returns
void my_unload(void)
{// Add clean-up code…
}需要注意的是该共享库不能使用-nostartfiles 和 -nostdlib 进行编译否则构造、析构函数不会调用 共享库创建方法 代码要编译成PIC代码使用-fPIC链接时指定为动态库 -shared 参考 http://tdistler.com/2007/10/05/implementing-dllmain-in-a-linux-shared-library 转载于:https://www.cnblogs.com/D3Hunter/p/3175770.html