在linux上实现DllMain + 共享库创建方法
DllMain可以在dll加載到進(jìn)程、線程時(shí)調(diào)用,可以做些初始化、清理的工作
但在linux上沒(méi)有專門(mén)的函數(shù),可以使用gcc擴(kuò)張屬性__attribute__((constructor)) and __attribute__((destructor))來(lái)實(shí)現(xiàn)
類似于全局類變量,其構(gòu)造函數(shù)及析構(gòu)函數(shù)會(huì)在加載時(shí)自動(dòng)調(diào)用。
上述方法不能實(shí)現(xiàn)線程attach、detach,但對(duì)一般程序足夠了
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… }需要注意的是,該共享庫(kù)不能使用-nostartfiles 和 -nostdlib 進(jìn)行編譯,否則構(gòu)造、析構(gòu)函數(shù)不會(huì)調(diào)用
共享庫(kù)創(chuàng)建方法:
代碼要編譯成PIC代碼,使用-fPIC,鏈接時(shí)指定為動(dòng)態(tài)庫(kù) -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 + 共享库创建方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASIHttpRequest:创建队列、
- 下一篇: Spring JTA应用JOTM At