How to suppress warnings in external headers in Visual C++
Asked Answered
S

5

76

I'm starting a new BREW project, and I'd like to compile with Warning Level 4 (/W4) to keep the application code nice and clean. The problem is that the BREW headers themselves don't compile cleanly with /W4.

In gcc you can differentiate between application and system headers by using -I and -isystem, and then by default gcc doesn't report any compilation warnings in system headers. Is there an equivalent mechanism in Visual C++?

Superintendent answered 29/3, 2010 at 23:43 Comment(4)
vote for visualstudio.uservoice.com/forums/121579-visual-studio-ide/…Messinger
This^ UserVoice instance is no longer available.Lover
@HenryAloni userVoice for visual-studio was migrated to developercommunity.visualstudio.com however I was not able to find the migrated ticket using the search function there.Bois
See devblogs.microsoft.com/cppblog/…Bois
C
4

It seems like there is an answer to this.

this post talks about /external:I that can be used to include headers with a special set of warnings.

I have not tested it myself, but the blog post is from 2017.

Contumacious answered 11/3, 2021 at 18:41 Comment(0)
G
121

Use this method around (a) header(s) that you cannot or don't want to change, but which you need to include.

You can selectively, and temporarily disable all warnings like this:

#pragma warning(push, 0)
// Some include(s) with unfixable warnings
#pragma warning(pop)

Instead of 0 you can optionally pass in the warning number to disable, so something like:

#pragma warning(push)
#pragma warning(disable : 4081)
#pragma warning(disable : 4706)
// Some code
#pragma warning(pop)
Garment answered 29/3, 2010 at 23:45 Comment(9)
StackOverflow rocks. Thanks for the complete answer. This solution is relatively annoying because it has to be done correctly in every source file, whereas in gcc you just configure your makefile correctly once.Superintendent
@Bob: No prob, glad to help. You could try putting those includes inside a different file and putting your include warning disables around just that one file.Garment
That's not particularly practical for BREW either, because there isn't one single base header file (or small group of header files) to wrap with an intermediate header. A typical project will have many source and header files, each of which includes a different subset of the BREW headers. I could potentially make one super-header that includes ALL BREW headers, and then pre-compile it to keep builds efficient. This technique would only work for Visual C++, and BREW projects need to build with at least two compilers (Visual C++ for simulator builds, and gcc or ARM for handsets).Superintendent
As an additional note, it is possible to disable multiple warnings in a single line like this: #pragma warning( disable : 4081 4706 )Epp
warning(push, 0) doesn't seem to work from inside a cpp, only from inside a header.Sonasonant
@ybungalobill: Probably you instantiate template defined in a header. In this case I believe that warning will be produced at instantiation place (in .cpp file body), so your pragma directive doesn't cover it. But I may be wrong.Subarid
If you want to use #pragma inside #define you can do: #define DISABLE_BAD_WARN __pragma(warning( disable : 4244 4626 5027 5031 4514))Aba
I did this around external headers, like opencv2, but I'm still getting all warnings from it.Lessielessing
This doesn't work if the warnings are from usage of the header inside your code. For example when you use a template from the header, it make the compiler read through the headers again and warn about stuff.Unguiculate
S
23

Visual C++ team has just added support for warning levels in external headers. You can find the details in their blog post: Broken Warnings Theory.

In essence it does automatically what the suggestions here were recommending to do manually: pushes new warning level right before #include directive and pops it up right after. There are additional flags to specify locations of external headers, flag to treat all <> includes as external, #pragma system_header and a feature not available in Clang or GCC (as of this writing) to see warnings in external headers across template instantiation stack when the template was instantiated in the user code.

Besides the comments under that post, you can also find some useful discussion in a reddit announcement for that post.

Socinus answered 22/12, 2017 at 1:1 Comment(6)
Checking in: As of October 2019, this option is still flawed, but pretty useful nonetheless. It's a good basis, but still not nearly up to the level of -isystem or similar; at the moment, wrapping includes in warning-disable pragmas still handles certain things noticeably better.Sosthenna
@JustinTime2ReinstateMonica can you elaborate what exactly doesn't work?Interoffice
Unfortunately, @AlexeyAndronov, I can't remember what it was that didn't seem to work a few months ago; if I remember, and it still hasn't been fixed, I'll make a note of it here.Sosthenna
It seems that flag /external mentioned in the article was in a Preview version. I can't find any mention of it in the VS2019 compiler flags documentation... Does anyone know what happened to this flag?Holbein
@AlexeyAndronov /external:anglebrackets doesn't work with /analyze, infact none of the default include or excludepath libraries get excluded. It is just BAD!Joon
@Joon /analyze:external-Cupric
T
8

The /external:anglebrackets /external:W0 compiler flags disable warnings on the headers imported with #include <...>.

You can change the W0 to W1, W2, W3, or W4, to set a different warning level for those.

Thibaut answered 10/12, 2022 at 19:58 Comment(1)
closest to the best up-to-date answer!! I think, that's the best you can do nowadays in lieu of -isystemAtahualpa
R
6

I don't believe Visual C++ lets you differentiate. You can fake it by using #pragma warning around the include:

#pragma warning(push, 0)
#include "mywarningheader.h"
#pragma warning(pop)
Rosiorosita answered 29/3, 2010 at 23:51 Comment(2)
@einpoklum Differentiate between your application-specific headers and other (third-party, system, etc.) headers which you cannot change.Basic
I did this around external headers, still getting all warnings, from mat.hpp, etc.Lessielessing
C
4

It seems like there is an answer to this.

this post talks about /external:I that can be used to include headers with a special set of warnings.

I have not tested it myself, but the blog post is from 2017.

Contumacious answered 11/3, 2021 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.