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.
I am not familiar with Tensilica xtensa compiler (
xt-xcc
), but with standardgcc
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
usinggcc
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 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.Edit (2016): 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 usinggcc
version 5.0.1 (prerelease version included in Ubuntu 15.04) will give two warnings unless option-Werror=cpp
is included as well. So it seems that-Werror
with newer gcc does not anymore imply-Werror=cpp
like before and it needs to be provided separately if desired.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.
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.Unfortunately, there is no answer for my specific toolchain, or so says the engineers at Tensilica. They don't support #message, or #pramga 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. If I do, I'll come back and update the answer.
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!