4

We have a VS 2019 project which includes external source libraries which we add to our project. For instance we are using WTL (Windows Template Library). During compile time, these libraries produce huge numbers of warnings (particularly for members which do not have default values).

For those files, we would like to completely disable warnings, or at least specify which warnings should be excluded for certain directories, because

  1. We are not going to change that code
  2. There are so many warnings, that we can easily miss some from our own code that we should resolve

I saw a post about some flags that you should be able to pass to disable warnings for "external" files, but I do not see any options in the project settings.

bpeikes
  • 3,495
  • 9
  • 42
  • 80
  • Does [How to suppress warnings in external headers in Visual C++](https://stackoverflow.com/a/2541990/7582247) answer your question? – Ted Lyngmo Aug 28 '20 at 04:10
  • 1
    According to [Broken Warnings Theory](https://devblogs.microsoft.com/cppblog/broken-warnings-theory/) they've experimented with `/external:I ` and `/external:W` to be able to set a different warning level on external headers. In the article it's said that one should use `/experimental:external` and while my VS2019 still seems to recognize part of what I'm trying to do, I can't quite figure out if it's still supported. – Ted Lyngmo Aug 28 '20 at 14:45

1 Answers1

1

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 )
Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7
  • This is close, but we include these third party header files all over the place in many files based on where they are needed. I don't see how we avoid having to copy paste this code everywhere. – bpeikes Aug 28 '20 at 13:40
  • I tried your last example, using pragma warning(push) and pop in my stdafx.h, but still get warnings. – bpeikes Aug 28 '20 at 16:38
  • I take that back. I did a full rebuild, and it seems to work! Thanks. – bpeikes Aug 28 '20 at 16:45