在linux上实现DllMain + 共享库创建方法
生活随笔
收集整理的這篇文章主要介紹了
在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驱动分离分层的概念