95

What's the closest GCC equivalent to this MSVC preprocessor code?

#pragma warning( push ) // Save the current warning state. #pragma warning( disable : 4723 ) // C4723: potential divide by 0 // Code which would generate warning 4723. #pragma warning( pop ) // Restore warnings to previous state. 

We have code in commonly included headers which we do not want to generate a specific warning for. However, we want files which include those headers to continue to generate that warning (if the project has that warning enabled).

2
  • If the headers are installed to /usr/include or what have you gcc doesn't generate warnings for them by default. Commented Jun 15, 2010 at 15:29
  • Does this answer your question? How to disable GCC warnings for a few lines of code Commented Sep 23, 2023 at 0:51

3 Answers 3

36

The closest thing is the GCC diagnostic pragma, #pragma GCC diagnostic [warning|error|ignored] "-Wwhatever". It isn't very close to what you want, and see the link for details and caveats.

Sign up to request clarification or add additional context in comments.

5 Comments

Do you know what and where the rationale for not adding this feature might be? (I couldn't find it.) I find the warning push-disable-pop to be useful.
I don't really imagine that "not adding features" to gcc tends to have a rationale so much as an absence of anyone submitting a working patch.
It's not that nobody is willing to do the work for this kind of fine-grained warning control in gcc, or submit the code - I know of one major Silicon Valley corporation that already did this, and another that would have been delighted to pay somebody to do it and get the code into the stream. Rather, per a discussion with a guy who (as one of the gdb maintainers) is plugged into this stuff, the gcc maintainers have a philosophy: "If there's a warning, it's a bug, and you need to fix it." So (imo) it's a religious argument, and they control the code so they win.
To add to Bob's comment, the GCC developers have a history of disliking the #pragma directive, so anything that is GCC-specific is probably more likely to be implemented as an __attribute__((foo)).
new gcc (>=4.4) has #pragma GCC push_options so you can muck about with more than just diagnostics... gcc.gnu.org/onlinedocs/gcc/…
33

I've done something similar. For third-party code, I didn't want to see any warnings at all. So, rather than specify -I/path/to/libfoo/include, I used -isystem /path/to/libfoo/include. This makes the compiler treat those header files as "system headers" for the purpose of warnings, and so long as you don't enable -Wsystem-headers, you're mostly safe. I've still seen a few warnings leak out of there, but it cuts down on most of the junk.

Note that this only helps you if you can isolate the offending code by include-directory. If it's just a subset of your own project, or intermixed with other code, you're out of luck.


Here is the official documentation talking about how system headers don't normally generate warnings or errors: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wsystem-headers (emphasis added):

-Wsystem-headers

Print warning messages for constructs found in system header files. Warnings from system headers are normally suppressed, on the assumption that they usually do not indicate real problems and would only make the compiler output harder to read. Using this command-line option tells GCC to emit warnings from system headers as if they occurred in user code. However, note that using -Wall in conjunction with this option does not warn about unknown pragmas in system headers—for that, -Wunknown-pragmas must also be used.

2 Comments

Great tip. If using LLVM, add the -isystem flag under "Other C Flags" in the "Apple LLVM Compiler - Language" section.
@Tom Thanks for sharing. I cannot understand where to use your solution. Can you say a little bit more?
1

This is an expansion to Matt Joiner's answer.

If you don't want to spawn pragmas all over your code, you can use the _Pragma operator:

#ifdef __GNUC__ # define DIAGNOSTIC_ERROR(w) _Pragma("GCC diagnostic error \"" w "\"") # define DIAGNOSTIC_IGNORE(w) _Pragma("GCC diagnostic ignore \"" w "\"") # define DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") # define DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") #endif // (...) DIAGNOSTIC_ERROR("-Wuninitialized") foo(a); // Error DIAGNOSTIC_PUSH DIAGNOSTIC_IGNORE("-Wuninitialized") foo(a); // No error DIAGNOSTIC_POP foo(a); // Error 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.