cgcccompiler-warningspragma

How to disable GCC warnings for a few lines of code


In Visual C++, it's possible to use #pragma warning (disable: ...). Also I found that in GCC you can override per file compiler flags. How can I do this for "next line", or with push/pop semantics around areas of code using GCC?


Solution

  • It appears this can be done. I'm unable to determine the version of GCC that it was added, but it was sometime before June 2010.

    Here's an example:

    #pragma GCC diagnostic error "-Wuninitialized"
        foo(a);         /* error is given for this one */
    
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wuninitialized"
        foo(b);         /* no diagnostic for this one */
    #pragma GCC diagnostic pop
    
        foo(c);         /* error is given for this one */
    #pragma GCC diagnostic pop 
    
        foo(d);         /* depends on command line options */