C++ 如何禁用特定的未知 #pragma 警告?(海湾合作委员会和/或叮当声)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6557042/
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
How to disable a particular unknown #pragma warning? (GCC and/or clang)
提问by CygnusX1
I know how to disable allunknown #pragma warnings. The answer was given, for example, here: SO: How to disable #pragma warnings?
我知道如何禁用所有未知的 #pragma 警告。例如,这里给出了答案:SO: How to disable #pragma warnings?
My question is - is there a way to disable an 'unknown pragma' warning for one particular pragma? For example, if I disable warning for #pragma ugubugu
the following code:
我的问题是 - 有没有办法禁用一个特定 pragma 的“未知 pragma”警告?例如,如果我禁用#pragma ugubugu
以下代码的警告:
#pragma ugubugu
#pragma untiunti
int main() {return 0;}
when compiled with either:
编译时使用:
g++ pragma.cpp -Wall
clang++ pragma.cpp -Wall
should produce a single warning:
应该产生一个警告:
warning: ignoring #pragma untiunti
Maybe, for example, there is a simple way to register a custom pragma which would do nothing?
例如,也许有一种简单的方法可以注册一个什么都不做的自定义编译指示?
Would be great to know if there is such an option is Visual Studio too, but that is less important.
很高兴知道 Visual Studio 是否也有这样的选项,但这并不重要。
Thank you!
谢谢!
"but why ultimately he's playing with custom pragmas?"
“但为什么他最终会使用自定义编译指示?”
My source is parsed by two compilers. In one of those, there is a special #pragma
, that is unknown to the other. Of course, I could probably put #ifdef COMPILER_IDENTIFICATION_MACRO ... #endif
around every instance of the #pragma
but that would be cumbersome.
我的源代码由两个编译器解析。在其中一个中,有一个特殊的#pragma
,这是另一个未知的。当然,我可能可以放置#ifdef COMPILER_IDENTIFICATION_MACRO ... #endif
每个实例,#pragma
但这会很麻烦。
采纳答案by Matthew Slattery
I'm reasonably sure that there isn't any way to do this.
我有理由确信没有任何方法可以做到这一点。
Both GCC and Clang do have internal interfaces which allow the language frontend to register #pragma
handlers with the preprocessor - see GCC's libcpp/directives.c
and Clang's lib/Lex/Pragma.cpp
- but, as far as I can see, there is nothing which lets you modify which handlers are registered (beyond what is implied by the language variant you're compiling for) based on command line options.
GCC 和 Clang 都有内部接口,允许语言前端向#pragma
预处理器注册处理程序 - 请参阅 GCClibcpp/directives.c
和 Clang lib/Lex/Pragma.cpp
- 但是,据我所知,没有什么可以让您修改已注册的处理程序(除了暗示的内容之外)由您正在编译的语言变体)基于命令行选项。
I know how to disable allunknown #pragma warnings. The answer was given, for example, here: SO: How to disable #pragma warnings?
我知道如何禁用所有未知的 #pragma 警告。例如,这里给出了答案:SO: How to disable #pragma warnings?
Note that the highest voted answer is better than the accepted one there. -Wno-unknown-pragmas
can simply be added on the command line afteranything (like -Wall
) which turns the warning on.
请注意,投票最高的答案比那里接受的答案要好。 -Wno-unknown-pragmas
可以简单地在命令行上加入后任何东西(象-Wall
其接通警告)。
My source is parsed by two compilers. In one of those, there is a special
#pragma
, that is unknown to the other. Of course, I could probably put#ifdef COMPILER_IDENTIFICATION_MACRO ... #endif
around every instance of the#pragma
but that would be cumbersome.
我的源代码由两个编译器解析。在其中一个中,有一个特殊的
#pragma
,这是另一个未知的。当然,我可能可以放置#ifdef COMPILER_IDENTIFICATION_MACRO ... #endif
每个实例,#pragma
但这会很麻烦。
From a more philisophical viewpoint, I think this is really the right solution, cumbersome though it may be!
从更哲学的角度来看,我认为这确实是正确的解决方案,尽管它可能很麻烦!
It seems correct to me to hide any #pragma
from a compiler which is not expected to understand it in the way that you intend, given that the whole point of #pragma
is to provide a mechanism for invoking implementation-defined behaviour in the compiler.
对我来说,向#pragma
编译器隐藏任何内容似乎是正确的,因为编译器不希望以您想要的方式理解它,因为重点#pragma
是提供一种机制来调用编译器中的实现定义的行为。
(If you do end up doing this, note that Clang defines __clang__
, but bothGCC and Clang define __GNUC__
.)
(如果你最终做这个,注意锵定义__clang__
,但都GCC和锵定义__GNUC__
。)
回答by sffc
I assume you want to disable the pragma warnings because it's something that is valid on one platform but not another. If that's the case, you can use macros to selectively enable the pragma, eliminating the need to suppress the warning.
我假设您想禁用编译指示警告,因为它在一个平台上有效但在另一个平台上无效。如果是这种情况,您可以使用宏来有选择地启用编译指示,从而无需抑制警告。
For example, if you want the pragma on Visual C++ only, you can do:
例如,如果您只想要 Visual C++ 上的编译指示,您可以执行以下操作:
#if defined(_MSC_VER)
# define SAFE_PRAGMA_UGUBUGU __pragma(ugubugu)
#else
# define SAFE_PRAGMA_UGUBUGU
#endif
And then, you can write
然后,你可以写
SAFE_PRAGMA_UGUBUGU
#pragma untiunti
int main() {return 0;}
回答by Hernán
- Compilers do not allow custom pragmas because pragmas are (mostly) compiler and/or linker controlling directives. Since this is very close to a particular compiler implementation and features, what would be the application of "defining new pragmas" for the user? In fact, what available pragma directives are implemented on a particular compiler is totally vendor independent (there is no C++ standarization rule).
- May be you want to use pragmas for marking up special sections of your code (e.g to feed your own preprocessor) since you are asking for no-op directives. This can be done using the preprocessor (#defines).
- Another possibility for custom "markup" in C/C++ code e.g: #MY_PRAGMA is to use your own preprocessor before the C/C++ one.
- 编译器不允许自定义编译指示,因为编译指示(大部分)是编译器和/或链接器控制指令。由于这非常接近特定的编译器实现和功能,那么为用户“定义新编译指示”的应用是什么?事实上,在特定编译器上实现的可用 pragma 指令完全独立于供应商(没有 C++ 标准化规则)。
- 可能您想使用编译指示来标记代码的特殊部分(例如,为您自己的预处理器提供数据),因为您要求使用无操作指令。这可以使用预处理器 (#defines) 来完成。
- C/C++ 代码中自定义“标记”的另一种可能性,例如:#MY_PRAGMA 是在 C/C++ 预处理器之前使用您自己的预处理器。
An example of this type of processing is used for the QT library, non-standard Metaobject System which interacts with the Qt MOC compiler. This is used to expand some non-C++ constructs (for example Q_OBJECT, Q_PROPERTY ,etc) that is later fed with a valid syntax to the C++ compiler.
此类处理的一个示例用于 QT 库,即与 Qt MOC 编译器交互的非标准元对象系统。这用于扩展一些非 C++ 构造(例如 Q_OBJECT、Q_PROPERTY 等),这些构造稍后会以有效的语法提供给 C++ 编译器。