Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to disable all warnings with a pragma?

I've started a new project and have decided to make sure it builds cleanly with the /Wall option enabled. The only problem is not all 3rd party libraries (like boost) compile without warnings, so I've resorted to doing this in a shared header:

#pragma warning(push)

#pragma warning(disable:4820)
#pragma warning(disable:4619)
#pragma warning(disable:4668)
#pragma warning(disable:4625)
#pragma warning(disable:4626)
#pragma warning(disable:4571)
#pragma warning(disable:4347)
#pragma warning(disable:4640)
#pragma warning(disable:4365)
#pragma warning(disable:4710)
#pragma warning(disable:4820)
#pragma warning(disable:4350)
#pragma warning(disable:4686)
#pragma warning(disable:4711)
#pragma warning(disable:4548)

#include <boost/array.hpp>
#include <boost/assert.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/function.hpp>
#include <boost/integer.hpp>
#include <boost/optional.hpp>
#include <boost/regex.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/utility.hpp>
#include <boost/variant.hpp>

#pragma warning(pop)

This works well enough, but every time I add new boost headers I have to add whatever warnings they generate to the list. Is there a way to say disable all warnings for this stretch of code?

like image 531
Ferruccio Avatar asked Feb 08 '09 13:02

Ferruccio


People also ask

How do I stop compiler warnings?

In Solution Explorer, select the NuGet package you want to suppress compiler warnings for. From the right-click or context menu, select Properties. In the Suppress warnings box of the package's properties, enter the warning number you want to suppress for this package.

How do I turn off warnings in GCC?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall .


4 Answers

You can push/pop a low level of warning, like this:

#pragma warning(push, 0)        

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
// ...

#pragma warning(pop)

But know that it's not possible to disable all warnings. For example, some linker warnings are impossible to turn off.

like image 120
Assaf Lavie Avatar answered Sep 26 '22 02:09

Assaf Lavie


#pragma warning(disable:4820)
#pragma warning(disable:4619)
#pragma warning(disable:4668)

for less lines....

#pragma warning (disable : 4820 4619 4668)
like image 43
EvilTeach Avatar answered Sep 29 '22 02:09

EvilTeach


What I've done before is set the "W3" option rather than "Wall" then in each of my own source .cpp files I put

#pragma warning(push, 4)

at the top AFTER all the "#include..." lines and then

#pragma warning(pop)

as the very last line of the file.

This way you get level 4 warnings in your code and level 3 in 3rd party code that you can't do anything about.

like image 5
Matt Warren Avatar answered Sep 29 '22 02:09

Matt Warren


Using the technique described in the Assaf Lavie's answer it is possible to create helper macros:

#define DISABLE_ALL_WARNINGS_BEGIN \
    __pragma(warning(push, 0))

#define DISABLE_ALL_WARNINGS_END \
    __pragma(warning(pop))

They can be used in the following way (online demo):

DISABLE_ALL_WARNINGS_BEGIN
void foo(int a)
{
    // this function should generate the following warning
    // "warning C4100: 'a': unreferenced formal parameter"
    // unless it is wrapped inside "DISABLE_ALL_WARNINGS" section
}
DISABLE_ALL_WARNINGS_END
like image 4
PolarBear Avatar answered Sep 29 '22 02:09

PolarBear