Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C++

Getting pragmatic with warnings

Rate me:
Please Sign up or sign in to vote.
3.11/5 (3 votes)
25 Mar 2002CPOL4 min read 89.3K   18   10
An article on dealing with unwanted Build warnings

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 1Displays severe warning messages. This is the default.
Level 2Displays a less-severe level of warning message than Level 1.
Level 3Displays 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 4Displays 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).

//
#pragma warning ( disable : < warning number ><WARNING number>) 

//
Example:
//
#pragma warning ( disable : 4800 ) 
//

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

//
#pragma warning (disable : wn1 wn2 wn3 <ETC>)
//

(where wn1, wn2,wn3 are three different warning numbers)

Example:

//
#pragma warning ( disable : 4800 4705 )  
//

Will disable warnings 4800 and 4705.

You can wrap small areas of code (like a function definition) with statements such as;

//
#pragma warning  ( disable : 4800 )

void SomeFunction()
{
…
}

#pragma warning ( default : 4800 )
//

Or for multiple disables,

//
#pragma warning ( push )

#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 ) 
#pragma warning( disable : 4707 ) 

void SomeFunction()
{
…
}
#pragma warning (pop) // to restore the state of the warning
//

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) TMR
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIn Defense Pin
bryce1-Apr-02 14:58
bryce1-Apr-02 14:58 
GeneralThe opposite approach Pin
Jim A. Johnson1-Apr-02 5:11
Jim A. Johnson1-Apr-02 5:11 
GeneralRe: The opposite approach Pin
john_d10-Aug-09 2:47
john_d10-Aug-09 2:47 
GeneralDangerous practice Pin
Gary R. Wheeler29-Mar-02 2:21
Gary R. Wheeler29-Mar-02 2:21 
GeneralRe: Dangerous practice Pin
Tim Smith29-Mar-02 2:30
Tim Smith29-Mar-02 2:30 
GeneralRe: Dangerous practice Pin
Michael Dunn29-Mar-02 21:35
sitebuilderMichael Dunn29-Mar-02 21:35 
GeneralRe: Dangerous practice Pin
1-Apr-02 6:39
suss1-Apr-02 6:39 
GeneralRe: Dangerous practice Pin
Bilby1-Apr-02 21:00
Bilby1-Apr-02 21:00 
GeneralRe: Dangerous practice Pin
Philippe Mori8-Apr-02 8:27
Philippe Mori8-Apr-02 8:27 
GeneralRe: Dangerous practice Pin
Arnt Witteveen19-Apr-02 5:44
Arnt Witteveen19-Apr-02 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.