views:

97

answers:

3

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?

+2  A: 

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

Giuseppe Guerrini
+2  A: 

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.

polygenelubricants
+2  A: 

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:

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

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

  3. 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...)

Michael Burr
Since this is GCC, the warning can sometimes be interpreted as, "you forgot to mark your `static` function `__inline__` as well (`inline` if you're on C99). Go on, you know you want to, just give it a try". Still more like a "nagging" than a "warning", though. And I guess the spirit of the "unused function" warning is similar to the spirit in which certain kinds of unreachable code in Java are compilation errors (`throw new Exception(); return;`).
Steve Jessop
There may be a more subtle reason for this warning. The assumption is that the "static" keyword has been added inadvertitely, but the function was meant to be public. Note that if you are producing a static library, only the final user of the library will notice that the function is not exported (OK, you should test your library before distributing it, don't you? ;) ).
Giuseppe Guerrini