Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable a specific gcc pedantic warning?

I've googled and googled and either google is failing me or you can't do this. This warning comes up when you turn on -Wpedantic...

ISO C++ forbids zero-size array ‘variable’ [-Wpedantic]

I want to turn off this one warning, not all pedantic warnings. Normally, I'd just add -Wno-xyz but I can't find the flag name that relates to that warning. It's just not listed anywhere.

Are the pedantic warnings special in that you can't remove them individually?

like image 638
stu Avatar asked Feb 01 '18 15:02

stu


People also ask

How do I supress a warning in GCC?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall .

How do you suppress warnings in C++?

To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.

Which option of GCC inhibit all warning messages?

If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option. Inhibit all warning messages. Make all warnings into errors. Make the specified warning into an error.


1 Answers

The good news: You can do this. The bad news: You can't use any commandline option. The [-Wpedantic] at the end of the diagnostic tells you that -Wno-pedantic is the narrowest option that will disable the diagnostic, and that's no use to you if you want to preserve all other pedantic diagnostics.

You'll have to do it case-by-case with pragmas.

main.cpp

int main(int argc, char *argv[])
{
    int a[0];
    int b[argc];
    return sizeof(a) + sizeof(b);
}

This program provokes two -Wpedantic diaqnostics:

$ g++ -Wpedantic -c main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:6:12: warning: ISO C++ forbids zero-size array ‘a’ [-Wpedantic]
     int a[0];
            ^
main.cpp:8:15: warning: ISO C++ forbids variable length array ‘b’ [-Wvla]
     int b[argc];
               ^

-Wno-vla will suppress the second one. To suppress the first, you have to resort to:

main.cpp (revised)

int main(int argc, char *argv[])
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
    int a[0];
#pragma GCC diagnostic pop
    int b[argc];
    return sizeof(a) + sizeof(b);
}

With which:

$ g++ -Wpedantic -c main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8:15: warning: ISO C++ forbids variable length array ‘b’ [-Wvla]
     int b[argc];
               ^
like image 65
Mike Kinghan Avatar answered Nov 14 '22 07:11

Mike Kinghan