C++ 如何禁用#pragma 警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/132667/
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:06:11  来源:igfitidea点击:

How to disable #pragma warnings?

c++compiler-constructionwarningspragma

提问by Marcos Bento

While developing a C++ application, I had to use a 3rd party library which produced a huge amount of warnings related with a harmless #pragma directive being used.

在开发 C++ 应用程序时,我不得不使用一个 3rd 方库,该库产生了大量与正在使用的无害 #pragma 指令相关的警告。

../File.hpp:1: warning: ignoring #pragma ident
In file included from ../File2.hpp:47,
                 from ../File3.hpp:57,
                 from File4.h:49,

Is it possible to disable this kind of warnings, when using the GNU C++ compiler?

使用 GNU C++ 编译器时,是否可以禁用此类警告?

回答by Adam Wright

I believe you can compile with

我相信你可以编译

-Wno-unknown-pragmas

to suppress these.

压制这些。

回答by Airsource Ltd

In GCC, compile with -Wno-unknown-pragmas

在 GCC 中,使用 -Wno-unknown-pragmas 编译

In MS Visual Studio 2005 (this question isn't tagged with gcc, so I'm adding this for reference), you can disable globally in Project Settings->C/C++->Advanced. Enter 4068 in "Disable Specific Warnings"

在 MS Visual Studio 2005 中(这个问题没有用 gcc 标记,所以我添加了这个以供参考),你可以在项目设置->C/C++->高级中全局禁用。在“禁用特定警告”中输入 4068

or you can add this to any file to disable warnings locally

或者您可以将此添加到任何文件以在本地禁用警告

#pragma warning (disable : 4068 ) /* disable unknown pragma warnings */

回答by Mike Dimmick

Perhaps see GCC Diagnostic Pragmas? Alternatively in this case you could use the combination of optionsthat -Wallenables, excluding -Wunknown-pragmas.

也许请参阅GCC 诊断程序?另外在这种情况下,你可以使用选项组合-Wall使不计-Wunknown-pragmas

回答by pamplemousse_mk2

Thank you every one for the tip. In my case, I work with Qt Mingw. I need to set flag another way, in my .PRO file:

谢谢大家的提示。就我而言,我与 Qt Mingw 合作。我需要在我的 .PRO 文件中以另一种方式设置标志:

QMAKE_CXXFLAGS_WARN_ON += -Wno-unknown-pragmas

回答by nemequ

I know the question is about GCC, but for people wanting to do this as portably as possible:

我知道这个问题是关于 GCC 的,但对于想要尽可能便携地做到这一点的人来说:

Most compilers which can emit this warning have a way to disable the warning from either the command line (exception: PGI) or in code (exception: DMC):

大多数可以发出此警告的编译器都可以通过命令行(例外:PGI)或代码(例外:DMC)禁用警告:

  • GCC: -Wno-unknown-pragmas/ #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  • clang: -Wno-unknown-pragmas/ #pragma clang diagnostic ignored "-Wunknown-pragmas"
  • Intel C/C++ Compiler: -diag-disable 161/ #pragma warning(disable:161)
  • PGI: #pragma diag_suppress 1675
  • MSVC: -wd4068/ #pragma warning(disable:4068)
  • TI: --diag_suppress,-pds=163/ #pragma diag_suppress 163
  • IAR C/C++ Compiler: --diag_suppress Pe161/ #pragma diag_suppress=Pe161
  • Digital Mars C/C++ Compiler: -w17
  • 海湾合作委员会:-Wno-unknown-pragmas/#pragma GCC diagnostic ignored "-Wunknown-pragmas"
  • 叮当:-Wno-unknown-pragmas/#pragma clang diagnostic ignored "-Wunknown-pragmas"
  • 英特尔 C/C++ 编译器:-diag-disable 161/#pragma warning(disable:161)
  • PG: #pragma diag_suppress 1675
  • MSVC:-wd4068/#pragma warning(disable:4068)
  • 蒂:--diag_suppress,-pds=163/#pragma diag_suppress 163
  • IAR C/C++ 编译器:--diag_suppress Pe161/#pragma diag_suppress=Pe161
  • 数字火星 C/C++ 编译器: -w17

You can combine most of this into a single macro to use in your code, which is what I did for the HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMASmacro in Hedley:

您可以将其中的大部分内容组合成一个宏以在您的代码中使用,这就是我HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMASHedley 中为宏所做的:

#if HEDLEY_HAS_WARNING("-Wunknown-pragmas")
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
#elif HEDLEY_INTEL_VERSION_CHECK(16,0,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
#elif HEDLEY_PGI_VERSION_CHECK(17,10,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
#elif HEDLEY_GNUC_VERSION_CHECK(4,3,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
#elif HEDLEY_MSVC_VERSION_CHECK(15,0,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
#elif HEDLEY_TI_VERSION_CHECK(8,0,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
#elif HEDLEY_IAR_VERSION_CHECK(8,0,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
#else
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
#endif

Note that Hedley may have more complete information than this answer since I'll probably forget to update this answer, so if you don't want to just use Hedley (it's a single public domain C/C++ header you can easily drop into you project) you might want to use Hedley as a guide instead of the information above.

请注意,Hedley 可能有比这个答案更完整的信息,因为我可能会忘记更新这个答案,所以如果你不想只使用 Hedley(它是一个公共域 C/C++ 标头,你可以轻松地放入你的项目) 您可能希望使用 Hedley 作为指南而不是上述信息。

The version checks are probably overly pessimistic, but sometimes it's hard to get good info about obsolete versions of proprietary compilers, and I'd rather be safe than sorry. Again, Hedley's information may be better.

版本检查可能过于悲观,但有时很难获得有关过时版本的专有编译器的好信息,我宁愿安全也不要抱歉。同样,Hedley 的信息可能会更好。

Many compilers can also push/pop warnings onto a stack, so you can push, then disable them before including code you don't control, then pop so yourcode will still trigger the warning in question (so you can clean it up). There are macros for that in Hedley, too: HEDLEY_DIAGNOSTIC_PUSH/ HEDLEY_DIAGNOSTIC_POP.

许多编译器还可以将警告推送/弹出到堆栈上,因此您可以推送,然后在包含您无法控制的代码之前禁用它们,然后弹出这样您的代码仍然会触发有问题的警告(因此您可以清理它)。Hedley 中也有相关的宏:HEDLEY_DIAGNOSTIC_PUSH/ HEDLEY_DIAGNOSTIC_POP