How to Disable Warnings for Particular Include Files

Visual Studio disable warnings for files in specific directories

There are several ways to disable the warning:

  1. Project Properties->C/C++->General->Warning Level->select level

Here is the Warning Level:

  • Turn off all warnings (/W0): Turn off the display of all warning
    messages. Level 1 (/W1): Display serious warning messages. Level 2
    (/W2): Display level 1 warnings and some less serious warnings, such
    as warnings about hidden class members. This is the default warning
    level on the command line. Level 3 (/W3): Display level 2 warnings
    and some less serious warnings, such as warnings about expressions
    that always evaluate to true or false. Level 4 (/W4): Display all
    level 3 warnings and informational warnings.

Or you could choose to disable specific warnings in Project Properties->C/C++->Advanced->Disable Specific Warnings


  1. You could use warning pragma.

Syntax:

#pragma warning(
warning-specifier : warning-number-list
[; warning-specifier : warning-number-list ... ] )
#pragma warning( push [ , n ] )
#pragma warning( pop )

Also, you could refer to Microsoft about How to: Enable and Disable Code Analysis for Specific C/C++ Warnings.

To enable or disable a code analysis warning

2.1.Create a header file that lists all the code analysis warnings and their initial state, as shown in the following code:

// WarningState.h
#pragma warning ( default : 6001 )
#pragma warning ( disable : 6011 )
// more warnings here
// end of file

2.2.Include WarningState.h in the application header file. In this case, MyApplication.h represents the header file.

// MyApplication.h file
#include "WarningState.h"
// ...
// end of file

2.3.Include MyApplication.h file in the source code file. In this case, MyApplication.cpp represents the source file.

// MyApplication.cpp file
#include "MyApplication.h"

2.4.To modify the warning state, use the pragma warning-specifier in a .cpp file, as shown in the following code:

// MyApplication.cpp file
#include "MyApplication.h"
#pragma warning ( disable: 6001 )
#pragma warning ( default : 6001 )

To disable all code analysis warnings for included third-party files

Add the following code to your header file.

#include <codeanalysis\warnings.h>
#pragma warning( push )
#pragma warning ( disable : ALL_CODE_ANALYSIS_WARNINGS )
#include <third-party include files here>
#pragma warning( pop )

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!

In Xcode, how to suppress all warnings in specific source files?

Select your target and show Build Phases. Then enter the name of the file in the search box, and you should see it listed in the Compile Sources phase. Double-click in the Compiler Flags column for that file and enter -w to turn off all warnings for that file.

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.

Can you turn off (specific) compiler warnings for any header included from a specific location?

I hate to answer my own question here, but I'm afraid that the "correct" answer in this case is: it's not possible.



Related Topics



Leave a reply



Submit