Re: [問題] 請問vc中的__declspec(dllexport),GCC …

看板C_and_CPP作者 (原力副甦)時間15年前 (2010/06/04 08:42), 編輯推噓3(305)
留言8則, 4人參與, 最新討論串3/4 (看更多)
※ 引述《james732 (好人超)》之銘言: : 剛剛測試了一下,供你做參考 : 相關的知識可以看這個網站:http://tinyurl.com/yt3zmu : -- share.c 開始 -- : int add(int x, int y) { return x + y; } : -- share.c 結束 -- : 編譯指令:gcc share.c -shared -fPIC -o libshare.so : 編譯之後,把 libshare.so 這個檔案擺到 /usr/lib 底下 : (這個步驟有沒有更好的替代方案呢?) : -- test.c 開始 -- : int add(int x, int y); : int main() { int result = add(10, 20); } : -- test.c 結束 -- : 編譯指令:gcc test.c -lshare : 這樣 ./a.out 執行就沒有問題了 : (這是在 Linux 底下測試的,不知道 Mac 是不是相同) : (話說我不太懂 -fPIC 這個參數的意義…查了資料也不知所以然 orz) 我做了一個測試: ===== 被呼叫的程式, 編出來叫 libdll.dylib dll.h #ifndef _DLL_H_ #define _DLL_H_ extern "C" int multi (int a); #endif/*_DLL_H_*/ dll.cpp #include <stdio.h> int multi(int a) { printf("inside dll "); return 10*a; }/*multi*/ ==== 主程式==== #include <dlfcn.h> #include <stdio.h> typedef void* DLL; typedef int (FPTR*)(int); int main(void) { DLL handle = dlopen("libdll.dylib",RTLD_LAZY); if(NULL == handle){ printf("dylib does not exist\n"); return -1; }/*if*/ FPTR fptr = (FPTR)dlsym(handle,"multi"); int c = fptr(2); printf("c = %d\n",c); return 1; }/*main*/ 編的時候都沒問題,編好 dylib後 我把這動態庫 分別負制一份到主程式下面,包含release 與debug的資料夾下。 然後開始運行主程式. 怪意的在這裡: dll若是 debug產生的就完全正常,若是release就會找不到函數。 main使用debug模式並打開gdb,在dlopen是有load到東西,但是 鼠標放在handle 上面,顯示 DLL handle 0x8fe31d0 invalid 然後再運行下去,fptr內容值就是 0了(NULL) 試了一天都沒有結果啊QQ 還請大牛幫個忙。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.158.204 ※ 編輯: Risingforce 來自: 59.120.158.204 (06/04 16:43) ※ 編輯: Risingforce 來自: 59.120.158.204 (06/04 16:56)

06/04 19:09, , 1F
GCC 哪來的 debug 跟 release?你是不是搞混什麼東西了?
06/04 19:09, 1F

06/04 19:19, , 2F
他應該是指參數加上-g吧
06/04 19:19, 2F

06/04 19:23, , 3F
我覺得不是,至少不加 -g 不會有人講成是 release。
06/04 19:23, 3F

06/04 20:44, , 4F
我是用Xcode QQ
06/04 20:44, 4F

06/04 21:02, , 5F
果然是Xcode...想說剛剛在linux上沒辦法用....
06/04 21:02, 5F

06/04 21:02, , 6F
visibility的問題~~
06/04 21:02, 6F

06/04 22:49, , 7F
要看底層 command line 傳什麼參數才能進一步說明。
06/04 22:49, 7F

06/05 09:18, , 8F
linux下,大概跟gcc的版本有關。我是連工作站的~~
06/05 09:18, 8F
文章代碼(AID): #1C2Bo27l (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1C2Bo27l (C_and_CPP)