last day (15 days later) » 

10:00 PM
135
Q: Why should I always enable compiler warnings?

n.m.I often hear that when compiling C and C++ programs I should "always enable compiler warnings". Why is this necessary? How do I do that? Sometimes I also hear that I should "treat warnings as errors". Should I? How do I do that?

 
I suspect the people who tell you this have never spent an hour or more looking for the one actual error in pages of meaningless warnings.
 
@jamesqf The point is to fix the warnings, not to just have loads of them sitting around cluttering up the output.
 
I would remove the C related tags. It would make a great Q & A reference for any compiled language
 
@jamesqf If you have hundreds of thousands of lines of code that have been developed over years and never compiled with warnings enabled, then yes, turning on warnings at that point is either useless, or mandates a huge catch-up effort to fix them all (no matter what) in order to make them useful again. But that's almost completely beside the point of this question, which is to encourage people to enable warnings (and take them seriously) from the beginning, so that they don't get into horrible binds like that.
 
@Steve Summit: When you have hundreds of thousands of lines of code developed over decades, people change the definition of the language (or what the compiler writers think is the language), so that things that were perfectly acceptable in say 1990 now raise warnings. For instance using %x instead of %p to printf a pointer value.
 
10:00 PM
@jamesqf And what you should do in that case is so called "maintenance" of the code to keep it to current specs. ;) Obviously there are codebases where this isn't thought to be economical.
 
@jamesqf Yes, it's frustrating when the landscape changes and you have to change with it. (Me, I'm still bitter over the change from index to strchr.) But adapt with the times we must. I think you've misstated the issue with %p, though. What doomed your comfortable old usage of %x was not that someone declared that %p was better, but rather, that environments (such as the modern 64-bit ones) came along where sizeof(pointer) was not equal to sizeof(int). (But, yes, given that reality, %p really is better.)
 
What I try to do, personally, in an old code base with lots of existing warnings, is to enable -Wall in my local build, and then clean up warnings for the source files that I touch in new development or bug fix activities.
 
@jamesqf: You just happened to pick an example that's going to sink your argument. Using %x instead of %p was always a mistake. You just got away with it because you were developing for 32 bit flat. I cut my teeth on DOS and it was catastrophic failure in DOS.
 
@Joshua In fairness, using %x wasn't always a mistake, because there was a time when %p didn't exist. (And in those days, there were little battles of opinion between the VAX programmers, who liked to use %x, and the PDP-11 programmers, who liked to use %o.)
 
@SteveSummit: Still a mistake. Should have done "%x", (int)pointer instead.
 
10:00 PM
@Steve Summit: It is not a matter of "adapting with the times" - IOW changing the way you write new code. (Though that is a PITA in itself.) It's dealing with existing code bases that was written years, if not decades, before the language changes. Now if it's Fortran, I can (at least with the compilers I've used) handle the change just by naming the files with .f77, .f90, or whatever. Not with C, at least AFAIK.
@Joshua: You've just not been programming long enough, or dealing with old enough code. As Steve says, there was a time when %x (and many other printf format specifiers) didn't exist. As I said, it's the language that changes.
 
Author's notes. I have reverted all edits. This is the question I have wanted to ask and answer. If it's inappropriate (too broad or opinion based or whatever), please delete it, do not try to make it fit the quidelinesw. Ask and answer your own instead. Thank you.
 
@n.m. I think the first edit was appropriate.
 
In that case, @jamesqf, I would suggest only enabling all warnings once all reported errors are fixed. Recompiling with warnings enabled will then give you a good list of non-critical, but potentially troublesome, things to look into when you have the time.
 
@Justin Time: Of course. The point I was trying to make is that a doctrinaire "always enable compiler warnings" is just about as dumb as the other extreme of never enabling them. They're a tool: experience and common sense should tell you when it's appropriate to use that tool, and when not to.
 
@jamesqf "experience and common sense should tell you" This is true about pretty much everything. It should be pretty obvious that this Q&A is aimed at beginners, but I have added a paragraph addressing your concerns to my answer anyway.
@mevets I know the answer, that's why.
 
10:00 PM
Fair enough, @jamesqf. It can just be hard to tell how something's intended to be interpreted online, my bad.
 

last day (15 days later) »