How to Treat a Specific Warning as an Error

Can I treat a specific warning as an error?

This should do the trick: #pragma warning (error: 4715).

Or the /we4715 command line option (see /w, /W0, /W1, /W2, /W3, /W4, /w1, /w2, /w3, /w4, /Wall, /wd, /we, /wo, /Wv, /WX (Warning Level) (courtesy of Tom Sigerdas)).

Treat specific warning as error for C++ project in Visual Studio

Enter the numbers only, i.e. 4390. For multiple warnings, enter them semicolon separated: 4390;4391.

If you don't see it in the command line, click the "Apply" button.

In the command line, they will appear as /We"...".

How can I treat specific warnings as errors in C++ to be cross-platform?

For GCC and clang, the #pragma to elevate a specific warning to an error is very similar.

For GCC:

#pragma GCC diagnostic error "-Wunused-result"

For clang:

#pragma clang diagnostic error "-Wunused-result"

The Intel C/C++ compiler does, as you presume, support the MSVC-style #pragma (and it also defines the _MSC_VER macro, so you can use the same #if defined... block).

For "other" compilers, it's clearly very difficult to say – you would need to check the manual for each compiler you are likely to use. As far as I know, there is no standard (cross-platform) way to do this. Also note that, just because a compiler pre-defines the _MSC_VER macro, does not guarantee that it will also support all MSVC-style #pragma syntax.

Warning as error - How to get rid of these

Each project in Visual Studio has a "treat warnings as errors" option. Go through each of your projects and change that setting:

  1. Right-click on your project, select "Properties".
  2. Click "Build".
  3. Switch "Treat warnings as errors" from "All" to "Specific warnings" or "None".

The location of this switch varies, depending on the type of project (class library vs. web application, for example).

Treat all warnings as errors except... in Visual Studio

In Visual Studio 2022 we have a new Project Properties UI which includes an editor for this.

Under Build | Errors and Warnings if you set Treat warnings as errors to All, then another property appears which allows you to exempt specific warnings from being treated as errors:

Sample Image

This will add the following property to your project:

<WarningsNotAsErrors>618,1030,1701,1702</WarningsNotAsErrors>

How can I make this GCC warning an error?

I'm not sure what the correct warning is, but once you've found it, you can change its disposition with the following (using 'format' as the example):

#pragma GCC diagnostic error "-Wformat"

Or as strager points out:

gcc -Werror=format ...

I've checked the gcc source for this and this specific warning cannot be disabled via command line flags.

How can I compile without warnings being treated as errors?

Thanks for all the helpful suggestions. I finally made sure that there were no warnings in my code, but again was getting this warning from SQLite 3:

Assuming signed overflow does not occur when assuming that (X - c) <= X is always true

which I fixed by adding the CFLAG -fno-strict-overflow.

CMake: How to treat every resource compiler warning as error and to suppress specific warnings?

I don't believe either of these is possible. There are no documented general warning control options for the Windows Resource Compiler, just type rc /? to check.

You can change the Resource Compiler's flags using CMAKE_RC_FLAGS.

There is also a filter on COMPILE_FLAGS and COMPILE_OPTIONS that prevents them being used to pass any flags that are not defines or includes to the Resource Compiler. In principle, if you needed to, you could change the filter (CMAKE_RC_FLAG_REGEX) so as to be able to use these properties for more, but that would not help you in this situation.



Related Topics



Leave a reply



Submit