Why should I always enable compiler warnings?Why does C++ compilation take so long?When should static_cast,...
If you draw two cards in consecutively in a standard deck of 52 cards, what is the probability of getting black on the second draw?
Bit floating sequence
How do I politely hint customers to leave my store, without pretending to need leave store myself?
How is lower/no gravity simulated on a planet with gravity, without leaving the surface?
The speed of a boat is 5Km/h in still water. It crosses a river of width 1km along the shortest path in 15 minutes.
How to capture c-lightining logs?
How do I decide when to use MAPE, SMAPE and MASE for time series analysis on stock forecasting
Gap in tcolorbox after title
How would two worlds first establish an exchange rate between their currencies
Does the 2019 UA artificer need to prepare the Lesser Restoration spell to cast it with their Alchemical Mastery feature?
Need help to understand the integral rules used solving the convolution of two functions
I need to know information from an old German birth certificate
Aftermarket seats
Why should I always enable compiler warnings?
Why does low tire pressure decrease fuel economy?
A property between "separable" and "second countable"
Why is the the worst case for this function O(n*n)
What is this sticking out of my wall?
Problem with listing a directory to grep
Is a MySQL database a viable alternative to LDAP?
Isn't that (two voices leaping to C like this) a breaking of the rules of four-part harmony?
Why is it that I have to play this note on the piano as A sharp?
How can I finish my PhD?
What should I do about my non-English publications when applying to a University in an English-speaking country?
Why should I always enable compiler warnings?
Why does C++ compilation take so long?When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?Improve INSERT-per-second performance of SQLite?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why should C++ programmers minimize use of 'new'?Why are elementwise additions much faster in separate loops than in a combined loop?Why is processing a sorted array faster than processing an unsorted array?Reference - What does this error mean in PHP?Obfuscated C Code Contest 2006. Please explain sykes2.cCompiling an application for use in highly radioactive environments
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I often hear that when compiling C and C++ programs I always should "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?
c++ c warnings
add a comment |
I often hear that when compiling C and C++ programs I always should "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?
c++ c warnings
1
these questions and their answers are so canonical that they should be on the first page (in bold or otherwise emphasized) of any decend learning book !
– engf-010
8 hours ago
add a comment |
I often hear that when compiling C and C++ programs I always should "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?
c++ c warnings
I often hear that when compiling C and C++ programs I always should "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?
c++ c warnings
c++ c warnings
edited 9 hours ago
sepp2k
310k41 gold badges610 silver badges628 bronze badges
310k41 gold badges610 silver badges628 bronze badges
asked 9 hours ago
n.m.n.m.
77.4k8 gold badges90 silver badges179 bronze badges
77.4k8 gold badges90 silver badges179 bronze badges
1
these questions and their answers are so canonical that they should be on the first page (in bold or otherwise emphasized) of any decend learning book !
– engf-010
8 hours ago
add a comment |
1
these questions and their answers are so canonical that they should be on the first page (in bold or otherwise emphasized) of any decend learning book !
– engf-010
8 hours ago
1
1
these questions and their answers are so canonical that they should be on the first page (in bold or otherwise emphasized) of any decend learning book !
– engf-010
8 hours ago
these questions and their answers are so canonical that they should be on the first page (in bold or otherwise emphasized) of any decend learning book !
– engf-010
8 hours ago
add a comment |
3 Answers
3
active
oldest
votes
Why enable warnings?
C and C++ compilers are notoriously bad at reporting some common programmer errors by default. Among the errors commonly ignored are:
- forgetting to initialise a variable
- forgetting to
return
a value from a function - arguments in
printf
andscanf
families not matching the format string - a function is used without being declared beforehand (C only)
All of these and more can be detected and reported, but the programmer needs to explicitly request that by supplying compiler options.
How to enable warnings?
This depends on your compiler.
Microsoft C and C++ compilers understand switches like /W1
, /W2
, /W3
, /W4
and /Wall
. It is recommended to use at least /W3
. /W4
and /Wall
may cause warnings to be emitted for system header files, you don't want that, but if your project compiles cleanly with one of these options, go for it. These options are mutually exclusive.
Most other compilers understand options like -Wall
, -Wpedantic
and -Wextra
. -Wall
is essential and all the rest are recommended. These options can be used separately or all together.
Your IDE may have a way to enable these from the user interface.
Why treat warnings as errors? They are just warnings!
A compiler warning signals a potentially serious problem in your code. The problems listed above are almost always fatal; others may or may not be, but you want compilation to fail even if it turns out to be a false alarm. Investigate each warning, find the root cause, and fix it. In the case of a false alarm, work around it — that is, use a different language feature or construct so that the warning is no longer triggered. If this proves to be very hard, disable that particular warning on a case by case basis.
You don't want to just leave warnings as warnings even if all of them are false alarms. It could be OK for very small projects where the total number of warnings emitted is less than 7. Anything more, and it's easy for a new warning to get lost in a flood of old familiar ones. Don't allow that. Just cause all your project to compile cleanly.
How to treat warnings as errors?
This is again done with compiler switches. /WX
is for Microsoft, most others use -Werror
. In either case, the compilation will fail if there are any warnings produced.
2
I have posted this Q&A because I'm sick and tired of telling people to enable warnings. Now I can just point them here (or, if I'm in a particularly evil mood, close their question as a dupe). You are welcome to improve this answer or add your own!
– n.m.
9 hours ago
You can also use clang's -Weverything
– pmg
8 hours ago
warnings (and errors) also get 'lost' due to limits on how many messages gets notified or the compiler ignores duplicates of same warning.
– engf-010
8 hours ago
The only modifier I would add is that some warnings may not be helpful for your application. (I've seen warnings that the compiler added 2 bytes of padding between elements in a struct. The application was for prototyping, so a little wasted memory didn't bother us.) Treat all warnings as errors and then only disable a warning if you know why that warning won't help you.
– Kyle A
7 hours ago
I would maybe add something on the distinction between "programmer errors" i.e., logical errors between what you meant and what you told the compiler via your code (which may otherwise be valid C/C++), and "language errors", i.e. things that aren't a valid use of the language.
– JMAA
6 hours ago
|
show 4 more comments
C is, famously, a rather low-level language as HLLs go. C++, though it might seem to be a considerably higher-level language than C, still shares a number of its traits. And one of those traits is that the languages were designed by programmers, for programmers -- and, specifically, programmers who knew what they were doing.
[For the rest of this answer I'm going to focus on C. Most of what I'll say also applies to C++, though perhaps not as strongly. Although as Bjarne Stroustrup has famously said, "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."]
If you know what you are doing -- really know what you are doing -- sometimes you may have to "break the rules". But most of the time, most of us will agree that well-intentioned rules keep us all out of trouble, and that wantonly breaking those rules all the time is a bad idea.
But in C and C++, there are surprisingly large numbers of things you can do that are "bad ideas" but which aren't formally "against the rules". Sometimes they're a bad idea some of the time (but might be defensible other times); sometimes they're a bad idea virtually all of the time. But the tradition has always been not to warn about these things -- because, again, the assumption is that programmers know what they are doing, they wouldn't be doing these things without a good reason, they'd be annoyed by a bunch of unnecessary warnings.
But of course not all programmers really know what they're doing. And, in particular, every C programmer (no matter how experienced) goes through a phase of being a beginning C programmer. And even experienced C programmers can get careless and make mistakes.
Finally, experience has shown not only that programmers do make mistakes, but that these mistakes can have real, serious consequences. If you make a mistake, and the compiler doesn't warn you about it, and somehow the program doesn't immediately crash or do something obviously wrong because of it, the mistake can lurk there, hidden, sometimes for years, until it causes a really big problem.
So it turns out that, most of the time, warnings are a good idea, after all. Even the experienced programmers have learned (actually, it's "especially the experienced programmers have learned") that, on balance, the warnings tend to do more good than harm. For every time you did something wrong deliberately and the warning was a nuisance, there are probably at least ten times you did something wrong by accident and the warning saved you from further trouble. And most warnings can be disabled or worked around for those few times when you really want to do the "wrong" thing.
(A classic example of such a "mistake" is the test if(a = b)
. Most of the time, this is a mistake, so most compilers these days warn about it -- some even by default. But if you really wanted to both assign b
to a
and test the result, you can disable the warning by typing if((a = b))
.)
The second question is, why would you want to ask the compiler to treat warnings as errors? I'd say it's because of human nature, specifically, the all-too-easy reaction of saying "Oh, that's just a warning, that's not so important, I'll clean that up later." But if you're a procrastinator (and I don't know about you, but I'm a terrible procrastinator) it's easy to put off the necessarily cleanup for basically ever -- and if you get into the habit of ignoring warnings, it gets easier and easier to miss an important warning message that's sitting there, unnoticed, in the midst of all the ones you're ignoring.
So asking the compiler to treat warnings as errors is a little trick you can play on yourself to get around this human foible.
Personally, I'm not as insistent about treating warnings as errors. (In fact, if I'm honest, I can say that I virtually never enable that options in my "personal" programming.) But you can be sure I've got that option enabled at work, where our style guide (which I wrote) mandates its use. And I would say -- I suspect most professional programmers would say -- that any shop that doesn't treat warnings as errors in C is behaving irresponsibly, is not adhering to commonly-accepted industry best practices.
add a comment |
Some warning may mean possible semantic error in code or possible UB. E.g. ;
after if()
, unused variable, global variable masked by local, or comparison of signed and unsigned. Many warnings are related to static code analyzer in compiler or to breaches of ISO standard detectable at compile time, which "require diagnostics". While those occurrences may be legal in one particular case, they would be result of design issues most of time.
Some compilers, e.g. gcc, have a command line option to activate "warnings as errors" mode, it's a nice , if cruel, tool to educate novice coders.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57842756%2fwhy-should-i-always-enable-compiler-warnings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why enable warnings?
C and C++ compilers are notoriously bad at reporting some common programmer errors by default. Among the errors commonly ignored are:
- forgetting to initialise a variable
- forgetting to
return
a value from a function - arguments in
printf
andscanf
families not matching the format string - a function is used without being declared beforehand (C only)
All of these and more can be detected and reported, but the programmer needs to explicitly request that by supplying compiler options.
How to enable warnings?
This depends on your compiler.
Microsoft C and C++ compilers understand switches like /W1
, /W2
, /W3
, /W4
and /Wall
. It is recommended to use at least /W3
. /W4
and /Wall
may cause warnings to be emitted for system header files, you don't want that, but if your project compiles cleanly with one of these options, go for it. These options are mutually exclusive.
Most other compilers understand options like -Wall
, -Wpedantic
and -Wextra
. -Wall
is essential and all the rest are recommended. These options can be used separately or all together.
Your IDE may have a way to enable these from the user interface.
Why treat warnings as errors? They are just warnings!
A compiler warning signals a potentially serious problem in your code. The problems listed above are almost always fatal; others may or may not be, but you want compilation to fail even if it turns out to be a false alarm. Investigate each warning, find the root cause, and fix it. In the case of a false alarm, work around it — that is, use a different language feature or construct so that the warning is no longer triggered. If this proves to be very hard, disable that particular warning on a case by case basis.
You don't want to just leave warnings as warnings even if all of them are false alarms. It could be OK for very small projects where the total number of warnings emitted is less than 7. Anything more, and it's easy for a new warning to get lost in a flood of old familiar ones. Don't allow that. Just cause all your project to compile cleanly.
How to treat warnings as errors?
This is again done with compiler switches. /WX
is for Microsoft, most others use -Werror
. In either case, the compilation will fail if there are any warnings produced.
2
I have posted this Q&A because I'm sick and tired of telling people to enable warnings. Now I can just point them here (or, if I'm in a particularly evil mood, close their question as a dupe). You are welcome to improve this answer or add your own!
– n.m.
9 hours ago
You can also use clang's -Weverything
– pmg
8 hours ago
warnings (and errors) also get 'lost' due to limits on how many messages gets notified or the compiler ignores duplicates of same warning.
– engf-010
8 hours ago
The only modifier I would add is that some warnings may not be helpful for your application. (I've seen warnings that the compiler added 2 bytes of padding between elements in a struct. The application was for prototyping, so a little wasted memory didn't bother us.) Treat all warnings as errors and then only disable a warning if you know why that warning won't help you.
– Kyle A
7 hours ago
I would maybe add something on the distinction between "programmer errors" i.e., logical errors between what you meant and what you told the compiler via your code (which may otherwise be valid C/C++), and "language errors", i.e. things that aren't a valid use of the language.
– JMAA
6 hours ago
|
show 4 more comments
Why enable warnings?
C and C++ compilers are notoriously bad at reporting some common programmer errors by default. Among the errors commonly ignored are:
- forgetting to initialise a variable
- forgetting to
return
a value from a function - arguments in
printf
andscanf
families not matching the format string - a function is used without being declared beforehand (C only)
All of these and more can be detected and reported, but the programmer needs to explicitly request that by supplying compiler options.
How to enable warnings?
This depends on your compiler.
Microsoft C and C++ compilers understand switches like /W1
, /W2
, /W3
, /W4
and /Wall
. It is recommended to use at least /W3
. /W4
and /Wall
may cause warnings to be emitted for system header files, you don't want that, but if your project compiles cleanly with one of these options, go for it. These options are mutually exclusive.
Most other compilers understand options like -Wall
, -Wpedantic
and -Wextra
. -Wall
is essential and all the rest are recommended. These options can be used separately or all together.
Your IDE may have a way to enable these from the user interface.
Why treat warnings as errors? They are just warnings!
A compiler warning signals a potentially serious problem in your code. The problems listed above are almost always fatal; others may or may not be, but you want compilation to fail even if it turns out to be a false alarm. Investigate each warning, find the root cause, and fix it. In the case of a false alarm, work around it — that is, use a different language feature or construct so that the warning is no longer triggered. If this proves to be very hard, disable that particular warning on a case by case basis.
You don't want to just leave warnings as warnings even if all of them are false alarms. It could be OK for very small projects where the total number of warnings emitted is less than 7. Anything more, and it's easy for a new warning to get lost in a flood of old familiar ones. Don't allow that. Just cause all your project to compile cleanly.
How to treat warnings as errors?
This is again done with compiler switches. /WX
is for Microsoft, most others use -Werror
. In either case, the compilation will fail if there are any warnings produced.
2
I have posted this Q&A because I'm sick and tired of telling people to enable warnings. Now I can just point them here (or, if I'm in a particularly evil mood, close their question as a dupe). You are welcome to improve this answer or add your own!
– n.m.
9 hours ago
You can also use clang's -Weverything
– pmg
8 hours ago
warnings (and errors) also get 'lost' due to limits on how many messages gets notified or the compiler ignores duplicates of same warning.
– engf-010
8 hours ago
The only modifier I would add is that some warnings may not be helpful for your application. (I've seen warnings that the compiler added 2 bytes of padding between elements in a struct. The application was for prototyping, so a little wasted memory didn't bother us.) Treat all warnings as errors and then only disable a warning if you know why that warning won't help you.
– Kyle A
7 hours ago
I would maybe add something on the distinction between "programmer errors" i.e., logical errors between what you meant and what you told the compiler via your code (which may otherwise be valid C/C++), and "language errors", i.e. things that aren't a valid use of the language.
– JMAA
6 hours ago
|
show 4 more comments
Why enable warnings?
C and C++ compilers are notoriously bad at reporting some common programmer errors by default. Among the errors commonly ignored are:
- forgetting to initialise a variable
- forgetting to
return
a value from a function - arguments in
printf
andscanf
families not matching the format string - a function is used without being declared beforehand (C only)
All of these and more can be detected and reported, but the programmer needs to explicitly request that by supplying compiler options.
How to enable warnings?
This depends on your compiler.
Microsoft C and C++ compilers understand switches like /W1
, /W2
, /W3
, /W4
and /Wall
. It is recommended to use at least /W3
. /W4
and /Wall
may cause warnings to be emitted for system header files, you don't want that, but if your project compiles cleanly with one of these options, go for it. These options are mutually exclusive.
Most other compilers understand options like -Wall
, -Wpedantic
and -Wextra
. -Wall
is essential and all the rest are recommended. These options can be used separately or all together.
Your IDE may have a way to enable these from the user interface.
Why treat warnings as errors? They are just warnings!
A compiler warning signals a potentially serious problem in your code. The problems listed above are almost always fatal; others may or may not be, but you want compilation to fail even if it turns out to be a false alarm. Investigate each warning, find the root cause, and fix it. In the case of a false alarm, work around it — that is, use a different language feature or construct so that the warning is no longer triggered. If this proves to be very hard, disable that particular warning on a case by case basis.
You don't want to just leave warnings as warnings even if all of them are false alarms. It could be OK for very small projects where the total number of warnings emitted is less than 7. Anything more, and it's easy for a new warning to get lost in a flood of old familiar ones. Don't allow that. Just cause all your project to compile cleanly.
How to treat warnings as errors?
This is again done with compiler switches. /WX
is for Microsoft, most others use -Werror
. In either case, the compilation will fail if there are any warnings produced.
Why enable warnings?
C and C++ compilers are notoriously bad at reporting some common programmer errors by default. Among the errors commonly ignored are:
- forgetting to initialise a variable
- forgetting to
return
a value from a function - arguments in
printf
andscanf
families not matching the format string - a function is used without being declared beforehand (C only)
All of these and more can be detected and reported, but the programmer needs to explicitly request that by supplying compiler options.
How to enable warnings?
This depends on your compiler.
Microsoft C and C++ compilers understand switches like /W1
, /W2
, /W3
, /W4
and /Wall
. It is recommended to use at least /W3
. /W4
and /Wall
may cause warnings to be emitted for system header files, you don't want that, but if your project compiles cleanly with one of these options, go for it. These options are mutually exclusive.
Most other compilers understand options like -Wall
, -Wpedantic
and -Wextra
. -Wall
is essential and all the rest are recommended. These options can be used separately or all together.
Your IDE may have a way to enable these from the user interface.
Why treat warnings as errors? They are just warnings!
A compiler warning signals a potentially serious problem in your code. The problems listed above are almost always fatal; others may or may not be, but you want compilation to fail even if it turns out to be a false alarm. Investigate each warning, find the root cause, and fix it. In the case of a false alarm, work around it — that is, use a different language feature or construct so that the warning is no longer triggered. If this proves to be very hard, disable that particular warning on a case by case basis.
You don't want to just leave warnings as warnings even if all of them are false alarms. It could be OK for very small projects where the total number of warnings emitted is less than 7. Anything more, and it's easy for a new warning to get lost in a flood of old familiar ones. Don't allow that. Just cause all your project to compile cleanly.
How to treat warnings as errors?
This is again done with compiler switches. /WX
is for Microsoft, most others use -Werror
. In either case, the compilation will fail if there are any warnings produced.
edited 7 hours ago
answered 9 hours ago
n.m.n.m.
77.4k8 gold badges90 silver badges179 bronze badges
77.4k8 gold badges90 silver badges179 bronze badges
2
I have posted this Q&A because I'm sick and tired of telling people to enable warnings. Now I can just point them here (or, if I'm in a particularly evil mood, close their question as a dupe). You are welcome to improve this answer or add your own!
– n.m.
9 hours ago
You can also use clang's -Weverything
– pmg
8 hours ago
warnings (and errors) also get 'lost' due to limits on how many messages gets notified or the compiler ignores duplicates of same warning.
– engf-010
8 hours ago
The only modifier I would add is that some warnings may not be helpful for your application. (I've seen warnings that the compiler added 2 bytes of padding between elements in a struct. The application was for prototyping, so a little wasted memory didn't bother us.) Treat all warnings as errors and then only disable a warning if you know why that warning won't help you.
– Kyle A
7 hours ago
I would maybe add something on the distinction between "programmer errors" i.e., logical errors between what you meant and what you told the compiler via your code (which may otherwise be valid C/C++), and "language errors", i.e. things that aren't a valid use of the language.
– JMAA
6 hours ago
|
show 4 more comments
2
I have posted this Q&A because I'm sick and tired of telling people to enable warnings. Now I can just point them here (or, if I'm in a particularly evil mood, close their question as a dupe). You are welcome to improve this answer or add your own!
– n.m.
9 hours ago
You can also use clang's -Weverything
– pmg
8 hours ago
warnings (and errors) also get 'lost' due to limits on how many messages gets notified or the compiler ignores duplicates of same warning.
– engf-010
8 hours ago
The only modifier I would add is that some warnings may not be helpful for your application. (I've seen warnings that the compiler added 2 bytes of padding between elements in a struct. The application was for prototyping, so a little wasted memory didn't bother us.) Treat all warnings as errors and then only disable a warning if you know why that warning won't help you.
– Kyle A
7 hours ago
I would maybe add something on the distinction between "programmer errors" i.e., logical errors between what you meant and what you told the compiler via your code (which may otherwise be valid C/C++), and "language errors", i.e. things that aren't a valid use of the language.
– JMAA
6 hours ago
2
2
I have posted this Q&A because I'm sick and tired of telling people to enable warnings. Now I can just point them here (or, if I'm in a particularly evil mood, close their question as a dupe). You are welcome to improve this answer or add your own!
– n.m.
9 hours ago
I have posted this Q&A because I'm sick and tired of telling people to enable warnings. Now I can just point them here (or, if I'm in a particularly evil mood, close their question as a dupe). You are welcome to improve this answer or add your own!
– n.m.
9 hours ago
You can also use clang's -Weverything
– pmg
8 hours ago
You can also use clang's -Weverything
– pmg
8 hours ago
warnings (and errors) also get 'lost' due to limits on how many messages gets notified or the compiler ignores duplicates of same warning.
– engf-010
8 hours ago
warnings (and errors) also get 'lost' due to limits on how many messages gets notified or the compiler ignores duplicates of same warning.
– engf-010
8 hours ago
The only modifier I would add is that some warnings may not be helpful for your application. (I've seen warnings that the compiler added 2 bytes of padding between elements in a struct. The application was for prototyping, so a little wasted memory didn't bother us.) Treat all warnings as errors and then only disable a warning if you know why that warning won't help you.
– Kyle A
7 hours ago
The only modifier I would add is that some warnings may not be helpful for your application. (I've seen warnings that the compiler added 2 bytes of padding between elements in a struct. The application was for prototyping, so a little wasted memory didn't bother us.) Treat all warnings as errors and then only disable a warning if you know why that warning won't help you.
– Kyle A
7 hours ago
I would maybe add something on the distinction between "programmer errors" i.e., logical errors between what you meant and what you told the compiler via your code (which may otherwise be valid C/C++), and "language errors", i.e. things that aren't a valid use of the language.
– JMAA
6 hours ago
I would maybe add something on the distinction between "programmer errors" i.e., logical errors between what you meant and what you told the compiler via your code (which may otherwise be valid C/C++), and "language errors", i.e. things that aren't a valid use of the language.
– JMAA
6 hours ago
|
show 4 more comments
C is, famously, a rather low-level language as HLLs go. C++, though it might seem to be a considerably higher-level language than C, still shares a number of its traits. And one of those traits is that the languages were designed by programmers, for programmers -- and, specifically, programmers who knew what they were doing.
[For the rest of this answer I'm going to focus on C. Most of what I'll say also applies to C++, though perhaps not as strongly. Although as Bjarne Stroustrup has famously said, "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."]
If you know what you are doing -- really know what you are doing -- sometimes you may have to "break the rules". But most of the time, most of us will agree that well-intentioned rules keep us all out of trouble, and that wantonly breaking those rules all the time is a bad idea.
But in C and C++, there are surprisingly large numbers of things you can do that are "bad ideas" but which aren't formally "against the rules". Sometimes they're a bad idea some of the time (but might be defensible other times); sometimes they're a bad idea virtually all of the time. But the tradition has always been not to warn about these things -- because, again, the assumption is that programmers know what they are doing, they wouldn't be doing these things without a good reason, they'd be annoyed by a bunch of unnecessary warnings.
But of course not all programmers really know what they're doing. And, in particular, every C programmer (no matter how experienced) goes through a phase of being a beginning C programmer. And even experienced C programmers can get careless and make mistakes.
Finally, experience has shown not only that programmers do make mistakes, but that these mistakes can have real, serious consequences. If you make a mistake, and the compiler doesn't warn you about it, and somehow the program doesn't immediately crash or do something obviously wrong because of it, the mistake can lurk there, hidden, sometimes for years, until it causes a really big problem.
So it turns out that, most of the time, warnings are a good idea, after all. Even the experienced programmers have learned (actually, it's "especially the experienced programmers have learned") that, on balance, the warnings tend to do more good than harm. For every time you did something wrong deliberately and the warning was a nuisance, there are probably at least ten times you did something wrong by accident and the warning saved you from further trouble. And most warnings can be disabled or worked around for those few times when you really want to do the "wrong" thing.
(A classic example of such a "mistake" is the test if(a = b)
. Most of the time, this is a mistake, so most compilers these days warn about it -- some even by default. But if you really wanted to both assign b
to a
and test the result, you can disable the warning by typing if((a = b))
.)
The second question is, why would you want to ask the compiler to treat warnings as errors? I'd say it's because of human nature, specifically, the all-too-easy reaction of saying "Oh, that's just a warning, that's not so important, I'll clean that up later." But if you're a procrastinator (and I don't know about you, but I'm a terrible procrastinator) it's easy to put off the necessarily cleanup for basically ever -- and if you get into the habit of ignoring warnings, it gets easier and easier to miss an important warning message that's sitting there, unnoticed, in the midst of all the ones you're ignoring.
So asking the compiler to treat warnings as errors is a little trick you can play on yourself to get around this human foible.
Personally, I'm not as insistent about treating warnings as errors. (In fact, if I'm honest, I can say that I virtually never enable that options in my "personal" programming.) But you can be sure I've got that option enabled at work, where our style guide (which I wrote) mandates its use. And I would say -- I suspect most professional programmers would say -- that any shop that doesn't treat warnings as errors in C is behaving irresponsibly, is not adhering to commonly-accepted industry best practices.
add a comment |
C is, famously, a rather low-level language as HLLs go. C++, though it might seem to be a considerably higher-level language than C, still shares a number of its traits. And one of those traits is that the languages were designed by programmers, for programmers -- and, specifically, programmers who knew what they were doing.
[For the rest of this answer I'm going to focus on C. Most of what I'll say also applies to C++, though perhaps not as strongly. Although as Bjarne Stroustrup has famously said, "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."]
If you know what you are doing -- really know what you are doing -- sometimes you may have to "break the rules". But most of the time, most of us will agree that well-intentioned rules keep us all out of trouble, and that wantonly breaking those rules all the time is a bad idea.
But in C and C++, there are surprisingly large numbers of things you can do that are "bad ideas" but which aren't formally "against the rules". Sometimes they're a bad idea some of the time (but might be defensible other times); sometimes they're a bad idea virtually all of the time. But the tradition has always been not to warn about these things -- because, again, the assumption is that programmers know what they are doing, they wouldn't be doing these things without a good reason, they'd be annoyed by a bunch of unnecessary warnings.
But of course not all programmers really know what they're doing. And, in particular, every C programmer (no matter how experienced) goes through a phase of being a beginning C programmer. And even experienced C programmers can get careless and make mistakes.
Finally, experience has shown not only that programmers do make mistakes, but that these mistakes can have real, serious consequences. If you make a mistake, and the compiler doesn't warn you about it, and somehow the program doesn't immediately crash or do something obviously wrong because of it, the mistake can lurk there, hidden, sometimes for years, until it causes a really big problem.
So it turns out that, most of the time, warnings are a good idea, after all. Even the experienced programmers have learned (actually, it's "especially the experienced programmers have learned") that, on balance, the warnings tend to do more good than harm. For every time you did something wrong deliberately and the warning was a nuisance, there are probably at least ten times you did something wrong by accident and the warning saved you from further trouble. And most warnings can be disabled or worked around for those few times when you really want to do the "wrong" thing.
(A classic example of such a "mistake" is the test if(a = b)
. Most of the time, this is a mistake, so most compilers these days warn about it -- some even by default. But if you really wanted to both assign b
to a
and test the result, you can disable the warning by typing if((a = b))
.)
The second question is, why would you want to ask the compiler to treat warnings as errors? I'd say it's because of human nature, specifically, the all-too-easy reaction of saying "Oh, that's just a warning, that's not so important, I'll clean that up later." But if you're a procrastinator (and I don't know about you, but I'm a terrible procrastinator) it's easy to put off the necessarily cleanup for basically ever -- and if you get into the habit of ignoring warnings, it gets easier and easier to miss an important warning message that's sitting there, unnoticed, in the midst of all the ones you're ignoring.
So asking the compiler to treat warnings as errors is a little trick you can play on yourself to get around this human foible.
Personally, I'm not as insistent about treating warnings as errors. (In fact, if I'm honest, I can say that I virtually never enable that options in my "personal" programming.) But you can be sure I've got that option enabled at work, where our style guide (which I wrote) mandates its use. And I would say -- I suspect most professional programmers would say -- that any shop that doesn't treat warnings as errors in C is behaving irresponsibly, is not adhering to commonly-accepted industry best practices.
add a comment |
C is, famously, a rather low-level language as HLLs go. C++, though it might seem to be a considerably higher-level language than C, still shares a number of its traits. And one of those traits is that the languages were designed by programmers, for programmers -- and, specifically, programmers who knew what they were doing.
[For the rest of this answer I'm going to focus on C. Most of what I'll say also applies to C++, though perhaps not as strongly. Although as Bjarne Stroustrup has famously said, "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."]
If you know what you are doing -- really know what you are doing -- sometimes you may have to "break the rules". But most of the time, most of us will agree that well-intentioned rules keep us all out of trouble, and that wantonly breaking those rules all the time is a bad idea.
But in C and C++, there are surprisingly large numbers of things you can do that are "bad ideas" but which aren't formally "against the rules". Sometimes they're a bad idea some of the time (but might be defensible other times); sometimes they're a bad idea virtually all of the time. But the tradition has always been not to warn about these things -- because, again, the assumption is that programmers know what they are doing, they wouldn't be doing these things without a good reason, they'd be annoyed by a bunch of unnecessary warnings.
But of course not all programmers really know what they're doing. And, in particular, every C programmer (no matter how experienced) goes through a phase of being a beginning C programmer. And even experienced C programmers can get careless and make mistakes.
Finally, experience has shown not only that programmers do make mistakes, but that these mistakes can have real, serious consequences. If you make a mistake, and the compiler doesn't warn you about it, and somehow the program doesn't immediately crash or do something obviously wrong because of it, the mistake can lurk there, hidden, sometimes for years, until it causes a really big problem.
So it turns out that, most of the time, warnings are a good idea, after all. Even the experienced programmers have learned (actually, it's "especially the experienced programmers have learned") that, on balance, the warnings tend to do more good than harm. For every time you did something wrong deliberately and the warning was a nuisance, there are probably at least ten times you did something wrong by accident and the warning saved you from further trouble. And most warnings can be disabled or worked around for those few times when you really want to do the "wrong" thing.
(A classic example of such a "mistake" is the test if(a = b)
. Most of the time, this is a mistake, so most compilers these days warn about it -- some even by default. But if you really wanted to both assign b
to a
and test the result, you can disable the warning by typing if((a = b))
.)
The second question is, why would you want to ask the compiler to treat warnings as errors? I'd say it's because of human nature, specifically, the all-too-easy reaction of saying "Oh, that's just a warning, that's not so important, I'll clean that up later." But if you're a procrastinator (and I don't know about you, but I'm a terrible procrastinator) it's easy to put off the necessarily cleanup for basically ever -- and if you get into the habit of ignoring warnings, it gets easier and easier to miss an important warning message that's sitting there, unnoticed, in the midst of all the ones you're ignoring.
So asking the compiler to treat warnings as errors is a little trick you can play on yourself to get around this human foible.
Personally, I'm not as insistent about treating warnings as errors. (In fact, if I'm honest, I can say that I virtually never enable that options in my "personal" programming.) But you can be sure I've got that option enabled at work, where our style guide (which I wrote) mandates its use. And I would say -- I suspect most professional programmers would say -- that any shop that doesn't treat warnings as errors in C is behaving irresponsibly, is not adhering to commonly-accepted industry best practices.
C is, famously, a rather low-level language as HLLs go. C++, though it might seem to be a considerably higher-level language than C, still shares a number of its traits. And one of those traits is that the languages were designed by programmers, for programmers -- and, specifically, programmers who knew what they were doing.
[For the rest of this answer I'm going to focus on C. Most of what I'll say also applies to C++, though perhaps not as strongly. Although as Bjarne Stroustrup has famously said, "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."]
If you know what you are doing -- really know what you are doing -- sometimes you may have to "break the rules". But most of the time, most of us will agree that well-intentioned rules keep us all out of trouble, and that wantonly breaking those rules all the time is a bad idea.
But in C and C++, there are surprisingly large numbers of things you can do that are "bad ideas" but which aren't formally "against the rules". Sometimes they're a bad idea some of the time (but might be defensible other times); sometimes they're a bad idea virtually all of the time. But the tradition has always been not to warn about these things -- because, again, the assumption is that programmers know what they are doing, they wouldn't be doing these things without a good reason, they'd be annoyed by a bunch of unnecessary warnings.
But of course not all programmers really know what they're doing. And, in particular, every C programmer (no matter how experienced) goes through a phase of being a beginning C programmer. And even experienced C programmers can get careless and make mistakes.
Finally, experience has shown not only that programmers do make mistakes, but that these mistakes can have real, serious consequences. If you make a mistake, and the compiler doesn't warn you about it, and somehow the program doesn't immediately crash or do something obviously wrong because of it, the mistake can lurk there, hidden, sometimes for years, until it causes a really big problem.
So it turns out that, most of the time, warnings are a good idea, after all. Even the experienced programmers have learned (actually, it's "especially the experienced programmers have learned") that, on balance, the warnings tend to do more good than harm. For every time you did something wrong deliberately and the warning was a nuisance, there are probably at least ten times you did something wrong by accident and the warning saved you from further trouble. And most warnings can be disabled or worked around for those few times when you really want to do the "wrong" thing.
(A classic example of such a "mistake" is the test if(a = b)
. Most of the time, this is a mistake, so most compilers these days warn about it -- some even by default. But if you really wanted to both assign b
to a
and test the result, you can disable the warning by typing if((a = b))
.)
The second question is, why would you want to ask the compiler to treat warnings as errors? I'd say it's because of human nature, specifically, the all-too-easy reaction of saying "Oh, that's just a warning, that's not so important, I'll clean that up later." But if you're a procrastinator (and I don't know about you, but I'm a terrible procrastinator) it's easy to put off the necessarily cleanup for basically ever -- and if you get into the habit of ignoring warnings, it gets easier and easier to miss an important warning message that's sitting there, unnoticed, in the midst of all the ones you're ignoring.
So asking the compiler to treat warnings as errors is a little trick you can play on yourself to get around this human foible.
Personally, I'm not as insistent about treating warnings as errors. (In fact, if I'm honest, I can say that I virtually never enable that options in my "personal" programming.) But you can be sure I've got that option enabled at work, where our style guide (which I wrote) mandates its use. And I would say -- I suspect most professional programmers would say -- that any shop that doesn't treat warnings as errors in C is behaving irresponsibly, is not adhering to commonly-accepted industry best practices.
edited 7 hours ago
answered 8 hours ago
Steve SummitSteve Summit
20.9k2 gold badges28 silver badges51 bronze badges
20.9k2 gold badges28 silver badges51 bronze badges
add a comment |
add a comment |
Some warning may mean possible semantic error in code or possible UB. E.g. ;
after if()
, unused variable, global variable masked by local, or comparison of signed and unsigned. Many warnings are related to static code analyzer in compiler or to breaches of ISO standard detectable at compile time, which "require diagnostics". While those occurrences may be legal in one particular case, they would be result of design issues most of time.
Some compilers, e.g. gcc, have a command line option to activate "warnings as errors" mode, it's a nice , if cruel, tool to educate novice coders.
add a comment |
Some warning may mean possible semantic error in code or possible UB. E.g. ;
after if()
, unused variable, global variable masked by local, or comparison of signed and unsigned. Many warnings are related to static code analyzer in compiler or to breaches of ISO standard detectable at compile time, which "require diagnostics". While those occurrences may be legal in one particular case, they would be result of design issues most of time.
Some compilers, e.g. gcc, have a command line option to activate "warnings as errors" mode, it's a nice , if cruel, tool to educate novice coders.
add a comment |
Some warning may mean possible semantic error in code or possible UB. E.g. ;
after if()
, unused variable, global variable masked by local, or comparison of signed and unsigned. Many warnings are related to static code analyzer in compiler or to breaches of ISO standard detectable at compile time, which "require diagnostics". While those occurrences may be legal in one particular case, they would be result of design issues most of time.
Some compilers, e.g. gcc, have a command line option to activate "warnings as errors" mode, it's a nice , if cruel, tool to educate novice coders.
Some warning may mean possible semantic error in code or possible UB. E.g. ;
after if()
, unused variable, global variable masked by local, or comparison of signed and unsigned. Many warnings are related to static code analyzer in compiler or to breaches of ISO standard detectable at compile time, which "require diagnostics". While those occurrences may be legal in one particular case, they would be result of design issues most of time.
Some compilers, e.g. gcc, have a command line option to activate "warnings as errors" mode, it's a nice , if cruel, tool to educate novice coders.
answered 6 hours ago
Swift - Friday PieSwift - Friday Pie
4,4711 gold badge11 silver badges27 bronze badges
4,4711 gold badge11 silver badges27 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57842756%2fwhy-should-i-always-enable-compiler-warnings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
these questions and their answers are so canonical that they should be on the first page (in bold or otherwise emphasized) of any decend learning book !
– engf-010
8 hours ago