Suppress Compiler warning Function declared never referenced

One solution is via function attributes.

void foo (int, int) __attribute__ ((unused));

This will tell gcc not to issue an unused function warning for the function foo. If you're worried about portability, you can define a macro UNUSED_FUNCTION_ATTRIBUTE that expands to __attribute__ ((unused)) with compilers that support attributes, but expands to nothing otherwise.


I'm fairly sure the relevant warning option is this one:

-Wunused-function
Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall.

So the warning should only be given for a static function, interesting. Makes sense. If a function is static it can only be used within the current file, so its definition must also be in this file.

And declaring it static inline avoids the warning, without resorting to ugly macros or compiler-specific pragmas or attributes.


...then I started to wonder if there was a different way to suppress that warning on that function.

There might be compiler option(s) to suppress this warning. However, one trick is this:

(void)foo; //cast it to void.

It should suppress this warning.

You could write a macro:

#define SUPPRESS_WARNING(a) (void)a

void foo(int thisIsAlsoAnUnsedParameter, int usedParameter)
{
   SUPPRESS_WARNING(foo); //better do this inside the definition itself :D

   SUPPRESS_WARNING(thisIsAlsoAnUnsedParameter);
}

As you can see, the definition of foo itself suppresses the warning.


In C++17 you can declare your function with [[maybe_unused]]:

[[maybe_unused]] void foo (int, int);

This will suppress the warning and is the correct, idiomatic way to express a possibly unused function in C++17.