我有一個常規的MFC應用程序,我想從中導出一個變量並在dll中使用它。以下是exe/dll中的聲明/定義摘要,其中應該包含here。從EXE中導出變量以便在DLL中使用時遇到問題
__declspec(dllexport) BOOL g_status; // In EXE
__declspec(dllimport) BOOL g_status; // in DLL
鏈接器錯誤是下面這似乎表明它無法找到由於名字改編?
error LNK2001: unresolved external symbol "__declspec(dllimport) int g_status" ([email protected]@3HA)
然後我嘗試這個組合的exe上側實際上forces它被成爲申報加入extern "C"' to declaration in dll but it doesn't work because if I use C decorations on dll, the same has to be done on exe side as well but using 'extern "C"
!
__declspec(dllexport) BOOL g_status; // in EXE
extern "C" __declspec(dllimport) BOOL g_status; // in DLL
鏈接如下錯誤:
error LNK2001: unresolved external symbol __imp__g_status
我有點失落。我看到很多帶有函數但帶有變量的例子,如何導出它們以及名稱裝飾仍在使用中的例子?
你的第一個「在EXE中」顯示'dllimport',應該是'dllexport' – franji1
@ franji1謝謝我驗證,這是一個錯誤的帖子而已,修正了它。 – zar
這幾乎總是以循環依賴的方式結束。除非鏈接EXE,否則無法鏈接DLL。但是,除非鏈接DLL,否則無法鏈接EXE。這是[正式可能](https://stackoverflow.com/questions/2727020/what-is-use-of-exp-and-what-is-the-difference-between-lib-and-dll),但不是直接受IDE支持。當然,最好避免,如果你需要另一個DLL作爲變量的起源,那麼就這樣吧。你可能會發現更多的用途。 –