在linux上实现DllMain + 共享库创建方法
生活随笔
收集整理的這篇文章主要介紹了
在linux上实现DllMain + 共享库创建方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
DllMain可以在dll加載到進(jìn)程、線程時調(diào)用,可以做些初始化、清理的工作
但在linux上沒有專門的函數(shù),可以使用gcc擴(kuò)張屬性__attribute__((constructor)) and __attribute__((destructor))來實(shí)現(xiàn)
類似于全局類變量,其構(gòu)造函數(shù)及析構(gòu)函數(shù)會在加載時自動調(diào)用。
上述方法不能實(shí)現(xiàn)線程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 進(jìn)行編譯,否則構(gòu)造、析構(gòu)函數(shù)不會調(diào)用
共享庫創(chuàng)建方法:
代碼要編譯成PIC代碼,使用-fPIC,鏈接時指定為動態(tài)庫 -shared
參考:?http://tdistler.com/2007/10/05/implementing-dllmain-in-a-linux-shared-library
?
轉(zhuǎn)載于:https://www.cnblogs.com/D3Hunter/p/3175770.html
總結(jié)
以上是生活随笔為你收集整理的在linux上实现DllMain + 共享库创建方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASIHttpRequest:创建队列、
- 下一篇: linux驱动分离分层的概念