无法暂时禁用GCC中的unknown-pragmas警告

问题描述 投票:2回答:1

我不能让方法暂时禁用GCC中的警告(参见How to disable GCC warnings for a few lines of code)工作,至少不能用于“unknown-pragmas”警告。

编译此代码......

#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个回答
1
投票

这是GCC C ++前端中一个长期缺失的功能:

使用g++中的程序无法控制预处理生成的警告。与C前端不同,只在C ++前端的预处理阶段之后处理编译指示。

© www.soinside.com 2019 - 2024. All rights reserved.