C++ #warning 预处理器指令的可移植性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/171435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:25:49  来源:igfitidea点击:

Portability of #warning preprocessor directive

c++compiler-constructionwarningsc-preprocessorportability

提问by jonner

I know that the #warning directive is not standard C/C++, but several compilers support it, including gcc/g++. But for those that don't support it, will they silently ignore it or will it result in a compile failure? In other words, can I safely use it in my project without breaking the build for compilers that don't support it?

我知道 #warning 指令不是标准的C/C++,但有几个编译器支持它,包括 gcc/g++。但是对于那些不支持它的人,他们会默默地忽略它还是会导致编译失败?换句话说,我可以在我的项目中安全地使用它而不破坏不支持它的编译器的构建吗?

采纳答案by Greg Hewgill

It is likely that if a compiler doesn't support #warning, then it will issue an error. Unlike #pragma, there is no recommendation that the preprocessor ignore directives it doesn't understand.

如果编译器不支持#warning,它很可能会发出错误。与#pragma 不同,不建议预处理器忽略它不理解的指令。

Having said that, I've used compilers on various different (reasonably common) platforms and they have all supported #warning.

话虽如此,我已经在各种不同(相当常见)的平台上使用过编译器,并且它们都支持#warning。

回答by nolandda

It should be noted that MSVC uses the syntax:

需要注意的是,MSVC 使用的语法是:

#pragma message ( "your warning text here" )

The usual #warning syntax generates a fatal error

通常的#warning 语法会产生致命错误

C1021: invalid preprocessor command 'warning'

so it is not portable to those compilers.

所以它不能移植到那些编译器。

回答by Jonathan Leffler

You are likely to get at least an unrecognized directive warning from compilers that don't recognize #warning, even if the code block is not included in your compilation. That might or might not be treated as an error - the compiler could legitimately treat it as an error, but many would be more lax.

即使代码块未包含在您的编译中,您也可能至少会从无法识别 #warning 的编译器中收到无法识别的指令警告。这可能会或可能不会被视为错误 - 编译器可以合法地将其视为错误,但许多会更加宽松。

Are you aware of (can you name) a compiler other than GCC/G++ that provides #warning? [Edited:Sun Solaris 10 (Sparc) and the Studio 11 C/C++ compilers both accept #warning.]

您是否知道(您能说出)除 GCC/G++ 之外的其他提供 #warning 的编译器吗?[已编辑:Sun Solaris 10 (Sparc) 和 Studio 11 C/C++ 编译器都接受 #warning。]

回答by Andrew Edgecombe

I had this problem once with a compiler for an Atmel processor. And it did generate preprocessor errors due to the unknown #warning token.

我曾经在使用 Atmel 处理器的编译器时遇到过这个问题。由于未知的#warning 标记,它确实产生了预处理器错误。

Unfortunately the solution seemed to be to convert the whole source tree to use the #pragma equivalent and accept that the build behavior was going to differ if using gcc.

不幸的是,解决方案似乎是将整个源代码树转换为使用#pragma 等效项,并接受如果使用 gcc,构建行为将有所不同。

回答by fantastory

When switching from mingw to visual studio, I added such lines to my global config header. (include it in stdafx.h)

从 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")

Now I use PRAGMA_WARNING(this need to be fixed)

现在我使用 PRAGMA_WARNING(这需要修复)

Sadly there is no #pragma warningin gcc, so it warns unspecified pragma.

遗憾的是#pragma warning,gcc 中没有,所以它警告未指定的编译指示。

I doubt that gcc will add #pragma warning"rather than microsoft adding #warning.

我怀疑 gcc 会添加#pragma warning"而不是微软添加#warning.

回答by 1800 INFORMATION

Actually most compilers that I know about ignore unknown #pragma directives, and output a warning message - so in the worst case, you'll still get a warning.

实际上,我所知道的大多数编译器都会忽略未知的 #pragma 指令,并输出警告消息 - 因此在最坏的情况下,您仍然会收到警告。