How do we disable specific warnings from C preprocessing - c

I have enabled -Werror for C pre processing command to treat warnings as errors.
Basically i wanted to only treat redefined warnings as errors. And all other warnings should not be treated as errors.
In file included from /home/test/version24/test.spec:35:0,
/test/release/base_version.spec:93:0: warning: "CD_MK_FILE" redefined
#define CD_MK_FILE 4
In file included from /home/test/version23/mss_litter.spec:19:0,
from
/test/release/base_version23.spec:0: note: this is the location of the previous definition
#define CD_MK_FILE 3
Is there any flag in to treat only redefined warnings as errors.
Thank you!

How do we enable or disable specific compiler warnings or errors?
For gcc/g++ and clang compilers, try:
# To disable -Werror just for warning -Wwhatever
-Wno-error=whatever
# To **enable** errors just for this one -Wwhatever warning
-Werror=whatever
For the clang compiler, you'd use -Wno-error=macro-redefined, to disable that error for the -Wmacro-redefined warning only, and you'd use -Werror=macro-redefined to enable an error only for that warning.
See here: https://clang.llvm.org/docs/DiagnosticsReference.html#wmacro-redefined. Thanks for the comment under the question, #user3386109!
See the list of all possible warnings and errors here:
For the gcc compiler: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
For the clang compiler (which is gcc-compatible, by design): https://clang.llvm.org/docs/DiagnosticsReference.html
For example, from the gcc link above:
-Werror=
Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.
The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)
Note that specifying -Werror=foo automatically implies -Wfoo. However, -Wno-error=foo does not imply anything.
Other References:
The comment by #HolyBlackCat
[my Q&A] How can I disable a C/C++ -Werror build error in Bazel? (AKA: how to turn OFF specific warnings already turned on by -Wall -Werror)
Related:
In case you ever need to control just a few lines of code, this is incredibly useful too: How to disable GCC warnings for a few lines of code

Related

Compiler option make error warning after -Wall -Werror

-Wall -Werror are already set.
I need to add some flag to make unused-s not error but warning. -Wno-unused discards warnings at all. I want to see warning and succeed compilation. How?
I tried: -Werror=no-unused but it does not work. GCC 9.
From the GCC manual:
-Werror=
Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.
So the option you want to pass is -Wno-error=unused.

C make file -Wno-unused-parameter flag

I am trying to compile some C code in Linux Kernel.
I have a huge project that compiles without any warnings. The compilation line is very very long and complex.
In project, the compiler did not warn about variables that are part of the function signature that were not used (even though it contains flags such as -Wall). such as:
void foo (int a) {
}
I cannot see why it would not warn on it.
So i checked the actual compilation lines on the original project by running:
make -n
It does not have the flag -Wno-unused-parameter that cancels this warning.
Does anyone know of a flag that cancels this warning? Maybe a different flag that contains the -Wno-unused-parameter flag ?
Thanks
Matt
According to Warning Options - Using the GNU Compiler Collection (GCC), -Wall doesn't enable -Wunused-parameter, so this warning isn't mysteriously cancelled, but simply not enabled.

Warnings while compiling a C library

I am trying to compile a C library and I am getting a loy of warning while doing it. Though the compiled library is working properly, I am still a bit apprehensive about the warnings. I googled all the warnings, but the relevant search results are hard to come by.
The warnings are:
main(){printf("osx%d", (int) (sizeof(void *)*8));}
^~~~
1 warning generated.
clang: warning: argument unused during compilation: '-s'
This warning is in the shell script that I used to compile the libraries. Could someone tell me what the -'s' flag is and how I can remove this warning?
Next warning:
warning: unknown warning option '-Wno-long-double'; did you mean '-Wno-long-long'? [-Wunknown-warning-option]
and the next one,
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: fortify.o has no symbols
clang: warning: argument unused during compilation: '-s'
Anyway to silence these warning?
Look at the manual of gcc for details of what each option should do.
AFAICT, clang not accepting those flags does not lead to any failure of the built executable.
Excerpt:
-s: Remove all symbol table and relocation information from the executable. (linker)
-Wno-long-double: Looks like someone wants to disable use of long double. Cannot find it for newest GCC. Chances are the code does not contain such anyway.

How does GCC behave if passed conflicting compiler flags?

I know that if you execute GCC as such:
gcc -O3 -O2 foo.c
GCC will use the last optimization flag passed (in this case O2). However, is this true for all flags? For example, if I execute GCC like so:
gcc -mno-sse -msse bar.c
Will it support SSE since that was the last flag passed, or would this result in undefined behavior? My initial experimentation seems to indicate that it will support SSE, but I'm not sure if this is true for all cases.
Normally later options on the line override ones passed previously, as you mention in your first example. I haven't personally come across any different behaviour for -m or -f flags, but I don't know of a specific reference in the documentation.
Note that some options don't behave this way:
$ gcc example.c -DABC -DABC=12
<command-line>: warning: "ABC" redefined
<command-line>: warning: this is the location of the previous definition
So there would need to be a -UABC in between there to shut that warning up.
As an aside, clang is particularly good at solving this problem - it will produce a warning if it ignores a command line option, which can help you out.

How to compile a Linux kernel module using -std=gnu99?

I've recently learned how to program simple character drivers and while playing around with the code I noticed that I get a lot of the following GCC warnings thrown for my C99 code:
warning: ISO C90 forbids mixed declarations and code
I assume this is because the main Linux kernel Makefile is set to compile using a non-C99 standard. I searched around I found this answer here on stackoverflow: How to use make and compile as C99?
So I naturally tried the following in my Makefile:
ccflags-y := -std=gnu99
Unfortunately this didn't silence the GCC warnings. I checked the verbose output of make and verified that GCC is indeed executed with the -std=gnu99 tacked on at the end; so I'm a bit confused.
How do I properly compile a Linux kernel module using the -std=gnu99 option?
EDIT:
I noticed the GCC output shows this option: -Wdeclaration-after-statement. Is this why I am getting the warnings even with the -std=gnu99 option?
It turns out that -std=gnu99 does in fact work; I began seeing errors regarding C99 features after removing the compiler flag. So that meant something else was causing the warnings to print out besides the -std= option.
After parsing through the verbose output via make V=1, I discovered the -Wdeclaration-after-statement option as part of the GCC execution. This was the cause of the ISO C90 mixed declaration warnings I saw.
To disable the ISO C90 warnings, pass this to GCC: -Wno-declaration-after-statement.
For example:
ccflags-y := -std=gnu99 -Wno-declaration-after-statement
You can also specify the flag in your Makefile, if you have one:
FLAGS=-std=gnu99

Resources