【问题标题】:Cannot temporarily disable unknown-pragmas warning in GCC无法在 GCC 中临时禁用未知编译指示警告
【发布时间】:2019-07-30 20:20:06
【问题描述】:

我无法使在 GCC 中暂时禁用警告的方法(请参阅 How to disable GCC warnings for a few lines of code)起作用,至少对于“未知编译指示”警告不起作用。

编译这段代码...

#pragma comment(user,"This should depend on the command line options")

#pragma GCC diagnostic warning "-Wunknown-pragmas"
#pragma comment(user,"This should cause a warning")

#pragma GCC diagnostic error "-Wunknown-pragmas"
#pragma comment(user,"This should cause an error")

#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma comment(user,"This should be ignored")

... 不产生警告/错误(除了链接器抱怨缺少main),或者当使用-Wall 或仅-Wunknown-pragmas 时,它会为每个注释编译指示产生一个警告。

我所期望的行为是每条评论都应该导致评论所说的内容。

我想我可以通过documentation 支持我的期望:

目前只能控制警告(通常由“-W...”控制),而不是所有警告。使用-fdiagnostics-show-option 确定哪些诊断是可控的,哪些选项控制它们。

我收到的警告显示为

warning: ignoring #pragma comment  [-Wunknown-pragmas]

正如括号中的部分告诉我们的那样,

  • 此诊断可控的
  • -Wunknown-pragmas 选项控制它

因此我的代码应该可以工作。

那我做错了什么?


版本信息:

$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609

【问题讨论】:

标签: gcc g++ pragma


【解决方案1】:

这是 GCC C++ 前端长期缺少的功能:

使用g++ 中的程序无法控制预处理产生的警告。与 C 前端不同,pragma 仅在 C++ 前端的预处理阶段之后进行处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2010-10-06
    • 2010-12-03
    相关资源
    最近更新 更多