-Werror 导致编译器在 #warning 时停止。我可以做什么来防止这种情况发生?
首先,我希望它在收到警告时停止。但我也想打印出一些信息性消息(例如“回来实施这个!”)。
不幸的是,我的编译器不支持 #info
、#message
、#pragma message()
等。
我知道有 - Wno-error=
,但我的 google-foo 很弱,我似乎找不到
的 #warning.我尝试过
-Wno-error=warning
,但那只是说“没有 -Wwarning
”。与“警告
”相同。
有什么建议吗?
就其价值而言,我正在使用 Tensilica Xtensa 编译器 xt-xcc,它出现成为 GNU 衍生产品,或者至少使用 GNU 前端。它的版本是8.0.0。
First off, I want it to stop on warnings. But I also want to print out some informative messages (like "Come back and implement this!").
Unfortunately, my compiler doesn't support #info
, #message
, #pragma message()
, etc.
I know there's -Wno-error=<something>
, but my google-foo is weak, and I can't seem to find out the <something>
for #warning
. I've tried -Wno-error=warning
, and that just says "there's no -Wwarning
". Same with "warn
".
Any suggestions?
For what its worth, I'm using the Tensilica Xtensa compiler, xt-xcc, which appears to be a GNU derivative, or at least uses the GNU front end. It's version 8.0.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我不熟悉 Tensilica Xtensa 编译器 (
xt-xcc
),但使用标准 GCC,您可以使用#warning
发出简单的警告(不是错误,因为-Werror
)。要使效果暂时有效,您可以在#pragma GCC 诊断推送
和#pragma GCC 之间使用此
。#pragma
和#warning
诊断弹出当我使用 GCC 4.6.1(命令
gcc -c -Werror warning-test.c
)编译包含以下内容的文件时,我得到以下输出:
当我删除第二个
#warning
时,编译不会因错误而中断。您还可以将
-Werror
编译器选项替换为-Werror -Wno-error=cpp
。我不知道cpp
警告类别还包括哪些其他影响(并且您可能在其他地方有一个合法的#warning
,您希望将其捕获为错误),所以暂时禁用特定#warning
的错误并立即恢复设置似乎是满足您的要求的更准确的方法。使用 GCC 版本 4.8.4 和 4.9.2 提供几乎相同的行为(仅额外打印出源代码行)。但使用 GCC 版本 5.0.1(预发布版本包含在 Ubuntu 15.04< /a> (Vivid Vervet)) 将给出两个警告,除非也包含选项
-Werror=cpp
。因此,似乎较新的 GCC 中的-Werror
不再像以前那样暗示-Werror=cpp
,并且如果需要,需要单独提供。I am not familiar with the Tensilica Xtensa compiler (
xt-xcc
), but with standard GCC you can useto make
#warning
a matter of simple warning (not an error because of-Werror
). To make the effect temporary, you can embrace this#pragma
and the#warning
between#pragma GCC diagnostic push
and#pragma GCC diagnostic pop
.When I compile a file containing the following
with
-Werror
using GCC 4.6.1 (commandgcc -c -Werror warning-test.c
), I get the following output:When I remove the second
#warning
, the compilation is not interrupted by an error.You can also replace your
-Werror
compiler options with-Werror -Wno-error=cpp
. I am not aware what other effects thecpp
category of warnings include (and you might have a legitimate#warning
in some other place that you want to catch as error), so temporarily disabling the error for a specific#warning
and restoring the settings immediately after seems more accurate method of meeting your requirements.Using GCC versions 4.8.4 and 4.9.2 gives pretty much the same behavior (only the source line is printed out additionally). But using GCC version 5.0.1 (prerelease version included in Ubuntu 15.04 (Vivid Vervet)) will give two warnings unless option
-Werror=cpp
is included as well. So it seems that-Werror
with a newer GCC does not anymore imply-Werror=cpp
like before and it needs to be provided separately if desired.十年后,发出警告并避免来自 -Wpedantic 的这些额外消息/错误的正确方法是使用
#pragma message
。 GCC 至少从 GCC 4.5 和 Clang 至少自 Clang 6。这些编译器的现代版本甚至支持
#pragma GCC 错误“消息”
和#pragma GCC 警告“消息”
(GCC 9,已在 Clang 6 中支持)。Ten years later, the proper way to issue warnings and avoid these additional messages/errors from -Wpedantic is to use
#pragma message
. GCC supports this syntax since at least GCC 4.5, and Clang since at least Clang 6.Modern versions of these compilers even support
#pragma GCC error "message"
and#pragma GCC warning "message"
(GCC 9, already supported in Clang 6).问题所在:
通常,编译器会在错误或警告消息中包含 - 的参数或 -
#warning
之后的材料。而且,通常情况下,如果编译器检测到需要警告的内容,它会相当清楚地报告它。
考虑到这些要求,我认为处理这个问题的唯一方法是保护
#warning
指令...大多数时候,您在编译时不使用
-DDO_WARNINGS
;当您需要检查#warning
警告(使用-Werror
)时,那么您毕竟要包含-DDO_WARNINGS
,接受编译将失败。请记住,即使存在个别编译错误,make -k
也会尽可能多地执行操作。GCC 4.4.1 手册第 5.52.9 节(部分)说:
我不确定您是否愿意将
#warning
行编辑为#pragma message
行。它可以帮助您解决问题 - 并且只会比在#warning
周围添加条件编译更糟糕,因为#pragma message
可能会受到较少编译器的支持。这取决于您的可移植性要求。What's wrong with:
Normally, the compiler will include the argument(s) to - or material after -
#warning
in the error or warning message.And, normally, if the compiler detects something that warrants a warning, it reports it fairly clearly.
Given the requirements, I think the only way to deal with this is to protect the
#warning
directives...Most of the time, you compile without
-DDO_WARNINGS
; when you need to check on the#warning
warnings (with-Werror
), then you include-DDO_WARNINGS
after all, accepting that the compilation will fail. Remember thatmake -k
will do as much as possible even in the presence of individual compilation errors.Section 5.52.9 of the GCC 4.4.1 manual says (in part):
I'm not sure whether you feel like editing your
#warning
lines into#pragma message
lines. It would get you around the problem - and is only worse than adding conditional compilation around the#warning
in that#pragma message
might be supported by fewer compilers. It depends on what your portability requirements are.鉴于 #warning 可能由预处理器处理,您可以单独运行预处理器而不使用 -Werror,然后运行编译器,并在预处理器输出上禁止预处理。
为此,通过预处理器使用除 -Werror 之外的所有正常选项运行 .c 文件,并将输出生成为 .i 文件(对于 C++ 为 .ii)。编译器将这些文件识别为不进行预处理,因此您可以使用 -Werror 编译它们,并假设预处理器删除 #warning 并且编译器本身不处理它,这可能会解决您的问题。
我还没有测试过这个;当我遇到同样的问题时,我只是选择忍受它而不是使用 -Werror。解决方案似乎比问题更复杂!
Given that #warning is presumably processed by the pre-processor, you could run the pre-processor separately without -Werror, then run the compiler with pre-processing inhibited on the pro-processor output.
To do this run the .c file through the preprocessor with all the normal options except -Werror, and generate the output as a .i file (.ii for C++). The compiler recognises these files as not to be pre-processed so you can compile them with -Werror, and assuming that the pre-processor drops the #warning and it is not processed by the compiler itself, this may resolve your problem.
I have not tested this; when I encountered the same problem I just chose to live with it and not use -Werror. The solution seemed more complex than the problem!
不幸的是,我的特定工具链没有答案,Tensilica 的工程师是这么说的。他们不支持 #message 或 #pragma message(),也不知道如何在存在 -Werror。
GCC 工具链允许使用 -Wno-error=[code] 来表示“此警告不是错误”,但我还没有找到与 #warning 对应的任何代码的列表采取(甚至可能是代码列表)。
我可能会尝试找时间深入研究标准 GCC 命令行和预处理器源代码,以尝试找到 -Wno-error= 可以等于的列表,或者是否存在 -Werror= 代码对应于#warning。
Unfortunately, there is no answer for my specific toolchain, or so says the engineers at Tensilica. They don't support #message, or #pragma message(), or know of how to suppress #warning as error in the presence of -Werror.
The GCC toolchain allows the use of -Wno-error=[code] to say "this warning is not an error", but I haven't found a list that corresponds #warning to any code this would take (or even a list of codes this could be).
I may try to find the time to delve into the standard GCC command lines and pre-processor source code to try to find a list of what -Wno-error= can be equal to, or whether or not there's a -Werror= code that corresponds to #warning.
如果您的编译器支持它,您可以尝试使用
constructor
函数属性 定义一个在程序启动时运行的函数(在main
之前),该函数将消息打印到 stdout:这会导致消息在运行时而不是编译时打印,这几乎一样好,特别是如果您没有其他选择。唯一的限制是您必须在函数定义之外的全局范围内使用它。
If your compiler supports it, you can try using the
constructor
function attribute to define a function that runs at program start (beforemain
) that prints out a message to stdout:This causes a message to be printed out at runtime instead of compile-time, which is almost as good, especially if you have no other options. The only restriction is that you have to use this at global scope outside of a function definition.
-Wno-error=#warnings
似乎适用于最近的 clang(测试 14..17)G++(11 和 12)不支持等效项,建议使用
--no-warnings
- 这不是你想要的。唯一的问题可能是让构建工具消化哈希,因此它会传递给编译器......
-Wno-error=#warnings
seem to work with recent clangs (tested 14..17)G++ (11 & 12) does not support the equivalent and suggest using
--no-warnings
- which is not what you want.The only issue might be to get the hash digested by your build tool, so it goes to the compiler...