How to: Enable and Disable Code Analysis for C/C++ Warnings
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

How to: Enable and Disable Code Analysis for C/C++ Warnings

To manage warning state, list all code analysis warnings in a separate header file. Include the header file in the source file. Use warning pragma to override the settings in the header file.


To enable or disable a code analysis warning

  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 ( enable : 6001 )
       #pragma warning ( disable : 6011 )
    // more warnings here 
    // end of file
  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
  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"
  4. To modify the warning state, use the pragma warning-specifier in a .cpp file, as shown in the following code:

    #pragma warning ( enable : 6011 )
    #pragma warning ( disable : 6001 )

Disable All Code Analysis Warnings

Your project may include files, such as third-party source code, for which you do not want to see code analysis warnings. The following code example disables all code analysis warnings for the included third-party files.

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