我知道#warning指令不是标准的 C / C ++,但有几个编译器支持它,包括gcc / g ++。但对于那些不支持它的人,他们会默默地忽略它还是会导致编译失败?换句话说,我可以在我的项目中安全地使用它而不破坏不支持它的编译器的构建吗?
答案 0 :(得分:71)
应该注意MSVC使用语法:
#pragma message ( "your warning text here" )
通常的#warning语法会产生致命错误
C1021: invalid preprocessor command 'warning'
所以它不能移植到那些编译器。
答案 1 :(得分:29)
如果编译器不支持#warning,则可能会发出错误。与#pragma不同,不建议预处理器忽略它不理解的指令。
话虽如此,我已经在各种不同(相当常见的)平台上使用了编译器,并且他们都支持#warning。
答案 2 :(得分:3)
即使代码块未包含在您的编译中,您也可能至少会从无法识别#warning的编译器中获得无法识别的指令警告。这可能会或可能不会被视为错误 - 编译器可以合法地将其视为错误,但许多人会更加宽松。
您是否知道(可以命名)GCC / G ++以外的编译器提供#warning? [已编辑: Sun Solaris 10(Sparc)和Studio 11 C / C ++编译器都接受#warning。]
答案 3 :(得分:1)
我曾经使用Atmel处理器的编译器遇到过这个问题。由于未知的#warning标记,它确实会产生预处理器错误。
不幸的是,解决方案似乎是将整个源代码树转换为使用#pragma等效项,并接受使用gcc时构建行为会有所不同。
答案 4 :(得分:1)
当从mingw切换到visual studio时,我将这些行添加到我的全局配置标题中。 (将其包含在stdafx.h中)
#ifdef __GNUC__
//from https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
//Instead of put such pragma in code:
//#pragma GCC diagnostic ignored "-Wformat"
//use:
//PRAGMA_GCC(diagnostic ignored "-Wformat")
#define DO_PRAGMA(x) _Pragma (#x)
#define PRAGMA_GCC(x) DO_PRAGMA(GCC #x)
#define PRAGMA_MESSAGE(x) DO_PRAGMA(message #x)
#define PRAGMA_WARNING(x) DO_PRAGMA(warning #x)
#endif //__GNUC__
#ifdef _MSC_VER
/*
#define PRAGMA_OPTIMIZE_OFF __pragma(optimize("", off))
// These two lines are equivalent
#pragma optimize("", off)
PRAGMA_OPTIMIZE_OFF
*/
#define PRAGMA_GCC(x)
// https://support2.microsoft.com/kb/155196?wa=wsignin1.0
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __PRAGMA_LOC__ __FILE__ "("__STR1__(__LINE__)") "
#define PRAGMA_WARNING(x) __pragma(message(__PRAGMA_LOC__ ": warning: " #x))
#define PRAGMA_MESSAGE(x) __pragma(message(__PRAGMA_LOC__ ": message : " #x))
#endif
//#pragma message "message quoted"
//#pragma message message unquoted
//#warning warning unquoted
//#warning "warning quoted"
PRAGMA_MESSAGE(PRAGMA_MESSAGE unquoted)
PRAGMA_MESSAGE("PRAGMA_MESSAGE quoted")
#warning "#pragma warning quoted"
PRAGMA_WARNING(PRAGMA_WARNING unquoted)
PRAGMA_WARNING("PRAGMA_WARNING quoted")
现在我使用PRAGMA_WARNING(需要修复)
可悲的是,gcc中没有#pragma warning
,所以它会警告未指定的编译指示。
我怀疑gcc会添加#pragma warning"
而非微软添加#warning
。
答案 5 :(得分:0)
实际上我所知道的大多数编译器会忽略未知的#pragma指令,并输出警告信息 - 所以在最坏的情况下,你仍然会收到警告。