2

In C code, if a function is used exclusively within a file, it's good practice to declare it to be static.

But sometimes I define a function to be static but don't actually use it (yet), in which case the compiler complains at me:

../src/app.c:79:12: error: 'foo' defined but not used [-Werror=unused-function]

What's the right way to tell the compiler (gcc in this case) that unused static functions are okay?

fearless_fool
  • 33,645
  • 23
  • 135
  • 217

4 Answers4

6

You can add the unused attribute to the function:

__attribute__((unused))
static void foo(void)
{
}
dbush
  • 205,898
  • 23
  • 218
  • 273
1

You can disable that specific error by providing this argument to GCC:

-Wno-unused-function

The error itself contained a hint that you could do this.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

I am lazy and standard compliant, and just do inline, and it suppresses the warning.

static inline void foo(void)
{
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

Somewhere in a function: (void)unusedFunctionName; This is how you can suppress unused variables warning for any variables and it is compiler independent (some compiler may still warn, but most probably don't and it doesn't break code for other compilers).