当前位置:
首页 >
在linux上实现DllMain + 共享库创建方法
发布时间:2023/11/30
56
豆豆
生活随笔
收集整理的这篇文章主要介绍了
在linux上实现DllMain + 共享库创建方法
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
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
总结
以上是生活随笔为你收集整理的在linux上实现DllMain + 共享库创建方法的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: ASIHttpRequest:创建队列、
- 下一篇: linux驱动分离分层的概念