BeeOnRope
BeeOnRope

Reputation: 64895

gcc warns about unused static functions, but not static inline: is there a practical distinction?

My version (5.4) of gcc warns about unused static functions, even in a header file when -Wall is used. It doesn't complain if the same functions are defined static inline or simply inline.

For example, the following function in a file unused.h:

static void foo() {}

... when included in a test.cpp file as follows:

#include "unused.h"

Generates the following compiler diagnostic when compiler with -Wall:

In file included from test.cpp:11:0:
unused.h: At global scope:
unused.h:9:13: warning: ‘void foo()’ defined but not used [-Wunused-function]
 static void foo() {}
             ^

It is common practice, as far as I know, to include headers with many utility functions, only a handful of which might be used in any given source file. This behavior means that I get warnings for any functions I don't use which are declared only static.

As a practical matter I can simply change these to static inline to get rid of the warning (or turn off the specific warning entirely, but I do find it useful from time to time), but it seems that large utility functions which won't benefit from inlining1 are more logically declared static2.

As far as I know unused static functions (just like static inline) are simply dropped by gcc when the translation unit is compiled, so they pose no binary size or link-time overhead at all.

Am I missing something here? Is there a good reason that unused static functions are more problematic than static inline?


1 Yes, I'm aware it's a hint only but gcc actually takes the hint in many cases.

2 Or perhaps better, only declared in the header file and defined somewhere else in a .cpp file - but that inhibits header-only use which is sometimes convenient.

Upvotes: 18

Views: 12523

Answers (2)

Cool Goose
Cool Goose

Reputation: 998

For such functions you need to set attribute __attribute__((unused)).

Upvotes: 0

M.M
M.M

Reputation: 141554

The warning is because an unused static function might indicate a logic error: why would you have written such a function if it was never called?

However, it is a common idiom to have static inline functions in a header file. Those functions might only be used by some translation units that include the header. It would be annoying if the compiler gave a warning for a translation unit that didn't happen to use one of the functions.

If you deliberately have an unused static non-inline function, you probably will want to either disable the warning entirely, or use a compiler-specific feature to suppress the warning for that function.


Someone asked, "why would you use static inline anyway?". Well, in new C++ you mostly wouldn't use it. However, in C it is a reasonable thing to do. (This is because static inline means the same thing in ISO C and GNU C; however inline without static behaves differently in ISO C than GNU C, so defaulting to static inline just avoids all those issues with no down-side).

People might use static inline in headers that are to be included from both .c and .cpp files; or perhaps they just carry over that habit from C to C++. In the latter case, IMHO it would be annoying for the compiler to warn about something that, although unnecessary, is not a mistake or a problem either.

Upvotes: 14

Related Questions