Mar 30, 2011

C++'s Most Misleading Compiler Option

-Wall

If you read that as "wall" then it's not too misleading, but it is kind of meaningless. If you read it as "Warnings all" it seems meaningful but it's really misleading. –Wmost would have been better, although I'm not sure if even that is accurate. I turn on –Wall and feel pretty smug when I compile without warnings. The smugness, it turns out,  is unwarranted. If you want to get closer to all add –Wextra. The specific dozen or so checks that are added are defined in the warning options docs.

Get Free Code Reviews From Scott Meyers whenever you want


For an extra dose of humility you can also add the -Weffc++ warning. The latter is defined in the dialect options docs. I have no idea why this is here rather than in the warnings documentation. I would have never found it except I was interested in reading what Scott Meyer's felt were the most important C++ books, and he mentioned it. This option adds warning for violations 0f items  11, 12, 14,15 and 23 of his Effective C++ and items 6 &7 of More Effective C++. 
Unfortunately, for either setting there is no way to turn off warnings for files from certain locations (like libraries).  Many files I don't have control over will not cleanly pass.  However, it's easy to separate those from the ones you care about. The path column is empty for the system libraries, and a path you should recognize will be present for your code as shown in the image.

image

 

Automating the process (somewhat)

Since you never want to leave warnings in your code what do you do? I would suggest the creations of a new configuration. The stricter warnings can be added for just that configuration. In the image you can see I named mine Strict Release. In the new configuration dialog, create a new configuration from the Release configuration. Then use the settings area as shown and manually entered the two new warning flags. Now when building you can use the Build icon to select either Release or Strict Release.  You can correct all the warnings you can under Strict and then switch to Release to get a clean warning free build. If you use lint  you can skip all this since lint has options to catch all of this items and more.  But if you don't want to spend the money you can at least get this small subset of the lint functionality right from your compiler.

SNAGHTML248d1b2d

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.