4

I'm trying to do something like this:

#ifdef _MSC_VER
    #define DISABLE_WARNINGS() \
        #pragma warning( push, 0 )
#elif __GNUC__
#define DISABLE_WARNINGS() \
        #define DISABLE_WARNINGS \
        #pragma GCC diagnostic push \
        #pragma GCC diagnostic ignored "-Wall"
#endif

I would like to define a single macro such as "DISABLE_WARNINGS" in my code before including 3rd party headers that produce enormous amount of warnings on W4, and also ensure the code compiles on any platform.

For example:

DISABLE_WARNINGS
#include <gtkmm/buttonbox.h>
#include <gtkmm/box.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
ENABLE_WARNINGS

What would be the best way to achieve this with single macro?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
codekiddy
  • 5,897
  • 9
  • 50
  • 80
  • I am not sure, but I doubt if you can have one `#define` macro containing more `#..` statements, especially separated by `\` Check this: http://stackoverflow.com/questions/3532309/multiple-preprocessor-directives-on-one-line-in-c Your macro is logically doing same thing. – anishsane Feb 26 '15 at 08:12
  • See my edit and then please give me a link to duplicate for g++ – codekiddy Feb 26 '15 at 08:26
  • 1
    @hvd, the duplicate doesn't answer how to deal with a conditional compilation between platforms (MS - GNU C), voting to reopen. – David Ranieri Feb 26 '15 at 08:29
  • @AlterMann Er... what? That's just a matter of putting the right `#define` between the right `#if`/`#endif`, something the OP already understands and put in the question already. –  Feb 26 '15 at 08:30
  • 1
    @codekiddy The other question *does* answer that already: use `_Pragma`. You already have the `#pragma` directives to use with GCC. –  Feb 26 '15 at 08:30
  • @hvd, in this way both ENABLE_WARNING and DISABLE_WARNING are defined, I think OP wants to enable/disable warnings under some condition, isn't it? – David Ranieri Feb 26 '15 at 08:35
  • @hvd I still can't see how to embed two _Pragma operators into one macro? GCC requires 2 times #pragma to disable warnings.. I may be wrong since I don't have g++ compiler right now to test it. – codekiddy Feb 26 '15 at 08:35
  • 2
    @codekiddy You can just use two `_Pragma`s in a row. –  Feb 26 '15 at 08:36
  • @hvd OK, thank's for that. I'll do so then. – codekiddy Feb 26 '15 at 08:38
  • @codekiddy BTW, I don't know if you're aware of this, but `#pragma GCC diagnostic ignored "-Wall"` only covers warnings that are enabled *directly* by `-Wall`, it doesn't cover warnings that are enabled by some other option that also gets enabled by `-Wall`. Depending on what you get, you may need to suppress other warnings as well (which is a simple matter of adding yet another `_Pragma`). –  Feb 26 '15 at 08:43
  • @hvd thanks, **warning( push, 0 )** is not able so disable all warning on MSVC on **Wall** either, in both cases it works to compile with W4 – codekiddy Feb 26 '15 at 09:06
  • For reference, I had closed this as a duplicate of [pragma in define macro](http://stackoverflow.com/questions/3030099/pragma-in-define-macro), and the question should have remained closed as a duplicate. (Which does not mean your question is a bad one.) I welcome anyone to re-close it. –  Feb 26 '15 at 09:13
  • 1
    @codekiddy You're not using the right syntax for `_Pragma`. As explained in the answer to that other question, `_Pragma` takes a string literal (unlike MSVC's `__pragma`). –  Feb 26 '15 at 09:14
  • @codekiddy `"GCC diagnostic ignored "-Wall""` is not a valid string literal. String literals cannot contain unescaped double quotes. –  Feb 26 '15 at 09:28
  • 1
    Please consider turning your EDIT2 into an answer. If you do I want to upvote it ! – Arnaud Nov 05 '18 at 21:15
  • [You're not supposed to use `/W4` anyway](https://learn.microsoft.com/en-us/previous-versions/thxezb7y(v=vs.140)?redirectedfrom=MSDN). Just lower your warning level to something more sensible. `/W2` or _maybe_ `/W3` is usually appropriate. – Lightness Races in Orbit Sep 16 '19 at 22:05

1 Answers1

2

In C99 mode, you can use _Pragma instead of #pragma:

#define DISABLE_WARNINGS \
    _Pragma("GCC diagnostic push") \
    _Pragma("GCC diagnostic ignored \"-Wall\"")
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226