When I compile C code in recent version of gcc, I am getting the following warning:
Function is define but not used.
What can be the reason for this warning and how can I approach to resolve it?
When I compile C code in recent version of gcc, I am getting the following warning:
Function is define but not used.
What can be the reason for this warning and how can I approach to resolve it?
It's probably a static function never used. If I am right, the warning says basically "you wrote some code you aren't actually using, so you are wasting memory and your program is bigger than needed".
Regards
The compiler is giving a warning that the programmer has put the effort of writing something that never gets used.
This can happen on purpose: perhaps the program is not yet complete, and as is good practice, the program is compiled early and compiled often, even before all the parts are connected.
This can also happen accidentally: perhaps the programmer made an honest mistake, that something was meant to be used but didn't, perhaps due to naming ambiguities etc.
Since it's not an error, the friendly compiler gives only a warning.
As Giuseppe Guerrini mentioned, this is likely for a static function - you won't see this warning on non-statics. The reason is that the compiler has to assume that a non-static function might be called from another compilation unit. however, a static function isn't visible outside of the C file it's in, so if it's not used in that file, then it can't be used at all.
If find myself generally irritated by these warnings because:
There are usually no negative side effects from having the function - even code space. Most linkers today will remove the code from the executable image if it's not used.
I often define a function in anticipation of it being used, and the warnings cause the build to, well, emit diagnostics. I prefer my builds to be warning-free, but in these cases I'd like the function to be compiled - ready to use when the next bit of work is gotten around to.
I may have code that gets called only in debug builds, like in an assertion (or some conditionally compiled code). However, I'm not a huge fan of conditional compilation and like to reduce the use of #if
statements to as few as possible. So my preference would be to have those functions not conditionally compiled even if they only get called in a particular configuration.
As far as I'm concerned there's little use to the warning, and it's probably OK to turn them off if you like. (I'd like to hear opinions that might change my mind...)