Niels Uiterwijk

Visual Studio C++ Compiler warnings as errors

At my day job I work for Codeglue on the porting for Terraria to mobile. We have an internal PC build where we develop most features on, and test it regularly on the current supported platforms: Android, iOs, Amazon (Kindle, Fire TV and Fire Phone) and Windows Phone.

One thing we noticed is that certain compilers are more strict then others, and for C++ usually the stricter the better. We notice a lot of times that the VS compiler is quite lenient. In our precompiled header we added the following lines:

#pragma warning (error: 4706 4701 4390 4716)
#pragma warning (disable : 4100 4201 4512 4389 4018 4189 4996 4702 4800 4245 4305 4238 4131)
Note: You need to make sure that you choose the ‘highest’ warning levels when compiling your code /W4

This code is only compiled if the target platform is win32, what this specifically enables is, that the following warnings are treated as errors:

These are often unwanted constructs in your code and can be solved almost always in a different way so it does not generate a compiler warning.

The disabled warnings are things that aren’t that important (except maybe the assignment operator). Such as ANSI compatibility and unused code.
In an ideal world you generally want to treat all warnings as errors, but when using 3rd party libraries this can be quite difficult. This way you atleast catch the important one’s. If you want to discuss you can always send me an email or tweet at me (@nielsuiterwijk)