2016-09-22 75 views
2

我正在使用mingw編譯OSX for Windows上的C++代碼。 C++代碼的自動生成,包括微軟的Visual Studio的特定代碼:C預處理器如何將函數宏視爲字符串

class __declspec(novtable) SomeClass 

當我編譯,我得到很多的警告:

warning: ‘novtable’ attribute directive ignored [-Wattributes] 

我想取消這些警告。 Mingw不支持-Wno-microsoft選項,所以我認爲我可能會將__declspec(notable)視爲指向空字符串的標識符,並讓預處理器將其刪除。

#define __declspec(novtable) 

然而,這被視爲__declspec()宏的重新定義,這是不期望的行爲。

有沒有辦法讓預處理器將__declspec(novtable)當作標識符,否則會禁止此警告? (有問題的自動生成的代碼不能修改)。

+0

你想要公關嗎?保留其他'__declspec'並忽略novtable? – Rup

+1

爲什麼你不能修改生成的代碼?在代碼上運行'sed'(或'awk')會相當方便。 – molbdnilo

+0

我很想修改代碼,但它是自動生成的,需要保持不變,以便它可以構建在多個平臺上。其他人可能希望用VC代碼來構建。 – John

回答

2

想必編譯器實際上

#define __declspec(x) __attribute__((x)) 

這個定義,也認識了一些(不是全部)微軟特定的屬性一樣dllexportdllimport。如果上述假設是真的,那麼以下情況纔有意義。

可以使用

#undef __declspec // to suppress a meessage about macro redefinition 
#define __declspec(x) // nothing 

(或許適宜#ifdef編以免中斷與MSVC兼容性)。

這將殺死整個__declspec功能,而不僅僅是__declspec(novtable)。如果這不是你需要的,請繼續閱讀。

如果你只需要殺死__declspec(novtable),並保留其他屬性不變,試試這個

#define novtable // nothing 

__attribute__指令可以包含屬性的可能是空列表,所以__declspec(novtable)大概會轉化爲__attribute__(()),這是非常好。這也將殺死標識符novtable的所有其他事件。如果確實發生在其他任何情況下,這種情況不太可能,但可能,這個選項不會起作用。

另一種可能性是接管整個功能。

#undef __declspec 
#define __declspec(x) my_attribute_ # x 

#define my_attribute_dllexport __attribute__((dllexport)) // or whatever you need 
#define my_attribute_dllimport __attribute__((dllimport)) // or whatever you need 
// ... same for all attributes you do need 

#define my_attribute_novtable // nothing 
// ... same for all attributes you don't need 
+0

這是_such_一個鈍器......! –

+0

@LightnessRacesinOrbit? –

+0

您爲整個後續代碼殺掉了整個'__declspec'功能,假設它是一個宏。 –

2

一些宏定義你的__declspec(novtable)

#define DECL_SPEC __declspec(novtable) 

之後,你可以使用它作爲:

class DECL_SPEC SomeClass 

並在需要時重新DECL_SPEC爲空:

#ifdef __MINGW32__ 
#define DECL_SPEC 
#else 
#define DECL_SPEC __declspec(novtable) 
#endif 
+1

Isn'倒退?他不想使用'class DECL_SPEC',他想在生成的代碼中忽略'__declspec(novtable)'而不發出警告。 (感覺就像你將#define參數的順序與'typedef'參數順序混淆在一起。) – Rup

+0

@Rup不,它根本沒有倒退。這個答案的意思是:「不要在你的代碼中生成'__declspec(novtable)',而是生成'DECL_SPEC',然後,你可以'#define DECL_SPEC'作爲必要的東西或者什麼也不做。 – Angew

+0

不確定如何忽略警告,@John可以嘗試'#pragma GCC診斷忽略'-Wattributes'' – Nikita