How to Suppress Specific Warnings in G++

How to suppress specific warnings in g++

Unfortunately, this feature isn't provided by g++. In VC++, you could use #pragma warning to disable some specific warnings. In gcc, the closest you can have is diagnostic pragmas, which let you enable/disable certain types of diagnostics for certain files or projects.

Edit: GCC supports pushing/popping warnings since 4.6.4 (see changelog)

How to disable a specific warning in g++

Ok, to summarize: the failure to suppress the warning is a known gcc bug (gcc.gnu.org/bugzilla/show_bug.cgi?id=61653). Since you cannot (and really, should not) suppress the warning, the easiest fix is to put a space between the literal and the #define string. You can safely do this; it won't change the output text.

The reason this is no longer allowed is because characters directly after a literal string are treated as user-defined literals, which is a new feature in C++11. User-defined literals are considered to be part of the same single token as the literal they modify, thus END will not be subject to replacement by the earlier #define.

How to supress warnings from some files when building with g++?

It depends how you're building your project, and which compiler. I'm assuming you're on Linux and you're using GCC. If not, similar techniques work for Visual Studio and other compilers too.

If you have a Makefile that you can easily modify, you can supply different compiler flags to build each file. Disabling a particular warning is as easy as adding a -Wno-<warning-name> to the build line, for example: -Wno-unused-local-typedefs.

If you can't easily modify your makefile, you can place #pragmas into your source code directly. For example, you can add a line like this to the top of the C++ file:

#pragma GCC diagnostic ignored "-Wno-unused-local-typedefs"

Of course, the real solution is to fix the warnings. There are very few warnings that aren't worth heeding: Often ignoring warnings will cause you more pain in the long run!

Selectively remove a warning message using GCC

If you need that code to work portable then you should cast the argument to unsigned int, as the int type may have a different size than Int32 on some platforms.

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx.

From the GCC Warning Options:

You can request many specific warnings with options beginning -W, for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning -Wno- to turn off warnings; for example, -Wno-implicit. This manual lists only one of the two forms, whichever is not the default.

For your case the warning in question is -Wformat

-Wformat

Check calls to printf and scanf, etc., to make sure that the arguments supplied have types appropriate to the format string specified, and that the conversions specified in the format string make sense. This includes standard functions, and others specified by format attributes (see Function Attributes), in the printf, scanf, strftime and strfmon (an X/Open extension, not in the C standard) families (or other target-specific families). Which functions are checked without format attributes having been specified depends on the standard version selected, and such checks of functions without the attribute specified are disabled by -ffreestanding or -fno-builtin.

The formats are checked against the format features supported by GNU libc version 2.2. These include all ISO C90 and C99 features, as well as features from the Single Unix Specification and some BSD and GNU extensions. Other library implementations may not support all these features; GCC does not support warning about features that go beyond a particular library's limitations. However, if -pedantic is used with -Wformat, warnings will be given about format features not in the selected standard version (but not for strfmon formats, since those are not in any version of the C standard). See Options Controlling C Dialect.

How to disable GCC warnings for a few lines of code

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 */

Disable all gcc warnings

-w is the GCC-wide option to disable warning messages.

How to suppress GCC warnings from library headers?

You may try to include library headers using -isystem instead of -I. This will make them "system headers" and GCC won't report warnings for them.



Related Topics



Leave a reply



Submit