Introduction
Have you ever compiled your masterfully written VC++ work of art, sat back and watched the compiler generate warning after senseless warning? I have, and I bet you have too. We’ve all seen the compiler fill up the build windows with truckloads of text ending with the annoying “2 error(s), 52 warning(s)” or some equally annoying number of warnings. After sorting our way through the messages usually you’ll find some of them are pretty boring. They may be technically correct but lets face it, some warnings just don’t impact significantly on the programming process.
So what can you do about it?
Well, you can tell the compiler to stop generating warnings.
How do you do this?
First, of course, head for “Project Settings” (that’s where all the fun can be found) Click on the “C/C++” tab where you’ll be able to suss out that you can modify the “Warning Level”. The most useful for this is Level 3. Code-heads (like Chris Maunder) probably click the “Warnings as Errors” checkbox but this is only for the real hard-core coders. You can experiment with this setting a bit, drop it down to “None” and rebuild your project. Notice then how there are no warnings issues. That’s cool but lets face it - only crazy coders are going to use that, especially on a Release build (but it’s a great trick to pull when the boss wanders by “yeah – of course - my code’s clean, now bugger off and sort out my pay rise” - just don’t forget to set it back after he has gone!).
According to my help files (abbreviated and with Microsoft’s™ bad spelling corrected)
|
Project Settings
|
Description
|
|
|
Level 1 |
Displays severe warning messages. This is the default. |
|
|
Level 2 |
Displays a less-severe level of warning message than Level 1. |
|
|
Level 3 |
Displays less-severe warnings than level 2, such as warnings about function calls that precede their function prototypes. Level 3 is the most sensitive warning level recommended for production purposes. |
|
|
Level 4 |
Displays informational warnings which in most cases can be safely ignored. Level 4 should be used occasionally to provide “lint” level warnings and is not recommended as your usual warning level setting. |
|
Its interesting to note that the help files say Level one is the default but the project settings seem to always start with Level 3 (on my PC anyway).
So what does this all mean? Well it means you can select the overall sensitivity of the compiler so that it generates “layers” or warnings. I personally prefer Level 3, but this still generates a mix of warnings I don’t want to know about but dropping to Level 2 means I miss out on warnings I DO like to see.
The #pragma warning directive
What can you do? How can you exclude the warnings you don’t want and keep the ones you do want? Well, its your lucky day for Microsoft™ not only provide the warnings but they also provide a way to turn them off –with a cool compiler directive called #pragma
.
The #pragma
directive is used with it’s warning disable option, this lets you tell the compiler which warnings you don’t want to know about (it also does loads of other neat stuff , but I’ll just concentrate on the warnings for this article).
The use of the #pragma
warning directive to control your warning reports is pretty straight-forward. In the file of interest (header or cpp file) put the following, (at the top of the file is obviously a good place).
<span class="cpp-comment"><span class="cpp-preprocessor">#pragma warning ( disable : < warning number ><WARNING number>) </span>
<span class="cpp-comment">
Example:
<span class="cpp-comment"><span class="cpp-preprocessor">#pragma warning ( disable : 4800 ) </span>
<span class="cpp-comment">
Will disable warning 4800 (forcing value to bool 'true' or 'false' (performance warning)).
You can find the warning number by checking the output of a build. You can also disable more than one warning at a time by either repeating the above code with a different warning number or by adding a list into the one line where the numbers are seperated by a space i.e.:
<span class="cpp-comment"><span class="cpp-preprocessor">#pragma warning (disable : wn1 wn2 wn3 <ETC>)</span>
<span class="cpp-comment">
(where wn1, wn2,wn3 are three different warning numbers)
Example:
<span class="cpp-comment"><span class="cpp-preprocessor">#pragma warning ( disable : 4800 4705 ) </span>
<span class="cpp-comment">
Will disable warnings 4800 and 4705.
You can wrap small areas of code (like a function definition) with statements such as;
<span class="cpp-comment"><span class="cpp-preprocessor">#pragma warning ( disable : 4800 )</span>
<span class="cpp-keyword">void</span> SomeFunction()
{
…
}
<span class="cpp-preprocessor">#pragma warning ( default : 4800 )</span>
<span class="cpp-comment">
Or for multiple disables,
<span class="cpp-comment"><span class="cpp-preprocessor">#pragma warning ( push )</span>
<span class="cpp-preprocessor">#pragma warning( disable : 4705 )</span>
<span class="cpp-preprocessor">#pragma warning( disable : 4706 ) </span>
<span class="cpp-preprocessor">#pragma warning( disable : 4707 ) </span>
<span class="cpp-keyword">void</span> SomeFunction()
{
…
}
<span class="cpp-preprocessor">#pragma warning (pop) <span class="cpp-comment">
The #pragma
warning directive can also be used for other purposes such as
|
Use
|
Example
|
|
|
Displaying the warning only once |
#pragma warning ( once : 4800 ) |
|
|
Apply the warning level (1-4) to the specified warning message(s). |
#pragma warning ( 3 : 4800 ) |
|
|
Report the specified warnings as errors |
#pragma warning ( error: 4800 ) |
|
Conclusion
There are times and places to turn your warnings off or to modify their levels. In most circumstances it’s usually quite obvious when modifying warning levels is a good idea, however, you should take care when using this tool - and do read the help files associated with the
#pragma
directives.