. : : 雅 舍 小 築 : : .


VS2005 warning C4251 needs to have dll-interface

分享 2008-10-16 15:24

寫程式看到warning有兩種辦法,消極一點就是不去理他,反正只是warning不是error。積極一點就是找辦法去消掉那個warning。消掉warning又有兩種辦法,一種是消極的用 #pragma warning(disable: xxxx) // replace xxxx with warning number 這種方式去鴕鳥一下,眼不見為淨,另一種就是誓死找出解法囉。

今天在code時剛好遇到一件這種事,翻了一大堆資料,解決了他,順便記下來,給大家參考參考。起因是這樣的,我正在用VS2005在coding,寫了Win32 application,其中有一隻DLL要export出來用。而在這DLL裡有我的一個自定型別,暫時稱為myStruct好了。而我在private變數中使用了std::vector<myStruct>去宣告了一個變數,在compile時就回給我warning C4251 needs to have dll-interface的warning了。找了一堆資料,起因是因為這個自定的型別也需要export出來才行。如果外部程式並不會去用到這個變數的話,其實忽略這個warning並不會怎樣。要消掉這個warning,就把這個型別也export出來就ok。

在設定project時,若勾了export library,VS2005會自動幫我們產生

#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

這樣的code,這時候可以用以下的方式來把這個型別給export出來。

template class MYDLL_API std::allocator<myStruct>;
template class MYDLL_API std::vector<myStruct, std::allocator<myStruct> >;

std::vector<myStruct> myStruct_Data;

只要加上紅色部分那兩行,就可以消除這個warning了。

以下有 0 則迴響 [發表迴響]
發表迴響
暱稱 (Name)
電子郵件 (E-mail)
(選填) (optional)
網頁 (Homepage)
(選填) (optional)
悄悄話 (Secret) 公開 (Public)
悄悄話, 請務必留E-mail (Secret, E-mail is required)
認證碼 (Auth Code)
(必填, 大小寫有別) (Required, case sensitive)
留言 (Message)
Logo 關於這裡的大小事 | Licenses | Contact Me | ©2008 Rical's Personal Studio