Can you turn off (specific) compiler warnings for

2020-08-09 10:51发布

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(...

5条回答
Emotional °昔
2楼-- · 2020-08-09 11:05

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

查看更多
聊天终结者
3楼-- · 2020-08-09 11:11

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
查看更多
【Aperson】
4楼-- · 2020-08-09 11:22

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.

查看更多
三岁会撩人
5楼-- · 2020-08-09 11:26

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

查看更多
不美不萌又怎样
6楼-- · 2020-08-09 11:27

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.

查看更多
登录 后发表回答