15

I've got a third-party library that generates a ton of warnings, even under /W3. Is there a way for me to tell the compiler, "disable C4244 for any file included from this directory, or its subdirectories"? Of course, I don't want to disable the warning in our own codebase, nor do I want to have to track down every possible include and wrap it with #pragma warning(...

moswald
  • 11,491
  • 7
  • 52
  • 78
  • 3
    Nice question :) I'd be glad if I could turn off warnings from Qt headers. – Vitor Py Jul 17 '10 at 20:38
  • @Vitor: I'm sure I have NO idea what you're talking about. ;) – moswald Jul 17 '10 at 22:29
  • I am not sure if a compiler can do it or not. But practically, you can filter these warnings after compiling. – czchen Jul 23 '10 at 00:49
  • @Praetorian has an answer that should work for you. Or you could use `#pragma warning(push)` `#pragma warning(disable:4244)` #include "3rdParty.h"` `#pragma warning(pop)` if you prefer. – Jesse Chisholm May 24 '17 at 18:19

5 Answers5

9

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

moswald
  • 11,491
  • 7
  • 52
  • 78
  • 1
    Yes. I've even tried to abuse the pre-processor to generate a INCLUDE macro that disabled warnings but that doesn't seem possible also. The only way out to have some sensible output I think is to grep the compiler warning output but for now I'm just living with all warnings. – Vitor Py Jul 21 '10 at 21:43
  • In Visual Studio 2015 (at least) there is a project `Properties / All Options / Disable specific warnings` you can set. The CLI for that is `/wd4244`. In other compilers it might be `/nowarn:4244` instead. – Jesse Chisholm May 24 '17 at 18:17
5

I'm not sure whether you meant you do not want to wrap your include statements with #pragma directives or did not want to spend time tracking down the right directive. If its the latter, then this is what I've done in the past:

#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#endif

#include "MyHeader.h"

#ifdef _MSC_VER
#pragma warning( default : 4244 ) /* Reset to default state */
#endif
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • Sorry, it was the former. I know how to disable warnings through #pragma already. – moswald Jul 17 '10 at 18:17
  • This should work. The compiler memorizes the warning pragma that was in effect when it compiled the declaration. – Hans Passant Jul 17 '10 at 18:29
  • 1
    @Hans: This might turn off the warnings generated by MyHeader.h, but it doesn't answer my question. I don't want to search a couple hundred files for the several dozen library includes, and then wrap them with the #pragmas. – moswald Jul 17 '10 at 18:53
  • 2
    @mos: why do you have to search? The compiler reminds you that you missed one. – Hans Passant Jul 17 '10 at 18:54
  • 1
    @mos: You don't have to manually search each one of your files to do this; just do a search & replace over the entire project / solution to insert the `#pragma` wrapper. You could even use some scripting language instead of Visual Studio's search and replace to provide a list of header files used by this library to make the task easier. And, as Hans has pointed out, the compiler will remind you of any you've missed. – Praetorian Jul 17 '10 at 22:55
  • This will cause warnings to show in some situations when their 'default; state is not desired. It is better to use `#pragma warning(push)` to save the warning state, then to change it, then `#pragma warning(pop)` to restore it. This way they are restored to whatever state they were in rather then the "default". – Sqeaky Feb 16 '16 at 16:35
  • Or add a `wrapper_3rdParty.h` to your own project, put the `#pragma warning(push)` `#pragma warning(disable:4244)` #include "real3rdParty.h"` `#pragma warning(pop)` in that wrapper and use that include in your project. – Jesse Chisholm May 24 '17 at 18:21
  • My issue is that the 3rd party header is also included from within their own classes so unless I edit their code to also wrap the include in warnings, it doesn't remove all of the warnings. – kinar Aug 16 '17 at 20:21
5

You can put flags e.g /wd4600 in VS Project Settings > Command-line Options to tell the complier to suppress specific Complier Warnings

cpx
  • 17,009
  • 20
  • 87
  • 142
3

You could try removing the 3rd party project from your include path. Then create a sub-dir that has the same dir structure and header files as the 3rd party project, so that all of the #includes now find your headers instead. Then in each fake header xxxx.h you set the pragma's then include the real xxxx.h header, then clear the pragma. To avoid recursively including the same file you would have to add an extra dir to the #include.

Personally, I'd just go through your project and add the pragma's.

jon hanson
  • 8,722
  • 2
  • 37
  • 61
3

Try:

//in wrapper_3rdParty.h
#pragma once

#pragma warning(push)
#pragma warning(disable : 4244)

#include "3rdParty.h"

#pragma warning(pop)

Then in your code, just #include "wrapper_3rdParty.h" instead.

That handles the issues with default behavior on the warning, and all insteances of your use of htat package have the warning suppressed.

Jesse Chisholm
  • 3,857
  • 1
  • 35
  • 29
  • To respond to all of your comments in one place: the library was large, and I would have had to write dozens of these includes. I was asking about a shortcut. – moswald May 25 '17 at 02:12