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 static
2.
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.
.cpp
file with the necessary implementations? The optimizer can decide what to do with them then. It's not clear why this is flagged asstatic
. Is it used elsewhere in the header file some how and important enough that it stay local to the file? – Bluenoseinline
as the silver bullet to fix their problems when really the best solution is to back up and evaluate why they're defining code inline in the first place. This tends to bloat your executable with duplicated copies of the same function if they are truly inlined. – Bluenoseinline
also serves as a hint that the function might be good for inlining, so I preferstatic
for functions that I don't want to hint for inlining (e.g., large or non-performance sensitive functions) and (b) in header files shared with Cinline
alone doesn't work there (sometimes an out-of-line function is required, so it's not header-only, etc). – Wednesdaygcc
in its newest version certainly does. Here are two identical functions (changed one constant so they aren't folded together) and gcc only inlines the one one markedinline
in an identical calling context). About (b) I'm sorry, I can't stand the flames anymore from tagging a question both C and C++. Because I have a valid reason to usestatic
functions that might involve interop with some other language doesn't mean I have to tag it. – Wednesday.cpp
file and in some other cases. For now I'm just addinginline
to make the functionsstatic inline
or I can use__attribute((unused))__
or othergcc
magic like#pragma GCC diagnostic ignored "-Wunused-function"
to make it go away on a case-by-case basis. The question is more about why gcc does this and if in particular I'm doing "something wrong" with mystatic
-but-not-inline
functions. – Wednesday-finline-functions
, neither explicitly nor implicitly through-O3
, so you are practically operating in "trust me, I know better" mode. You are, in the end, stopping gcc from taking the hint as a hint and mostly force it to treat it as a command instead (for functions that are not "small"). Disinterest about optimization flags does not justify by hand in-code optimization. – Zeke-O2
because various inlining options are active there (without inlining it would generate terrible code for all the "zero cost abstractions" that are prevalent in C++). You can try for yourself: make the function smaller and it will inline it, even withoutinline
and at-O2
. gcc uses the presence of aninline
specifier on a function in its inline cost heuristic. Is that hard to believe? I know it's vogue to say "Duh, compiler ignores inline!!!!" but it's no more true than the claim it is countering. – Wednesdayinline
,hot
,cold
,likely
,unlikely
,pure
, etc. We are getting more hints these days, not less. – Wednesdayinline
and I showed at the mainstream, most commonly used optimization level on the most recent version ofgcc
it plainly does not. That's all I have to do. Showing that thatinline
might not make a difference in some other scenario, with some other compilation flags is totally irrelevant. For my use case, it makes a difference, I showed you actual code, end of story. – Wednesday