【发布时间】: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
【问题讨论】: