Search code examples
c++cgccgcc-warning

Suppress Compiler warning Function declared never referenced


So i have some code like this:

void foo (int, int);

void bar ( )
{
    //Do Stuff

   #if (IMPORTANT == 1)
       foo (1, 2);
   #endif

}

When doing a compile without "IMPORTANT" I get a compiler Warning that foo is defined and never referenced. Which got me thinking (that is the problem).

So to fix this i just added the same #if (IMPORTANT == 1) around the function definition etc... to remove the warning, and then I started to wonder if there was a different way to suppress that warning on that function. I was looking at "unused" GCC attrib and didn't know if functions had the same attribute i could set? Is there even another way to suppress it that suppresses that warning for only that function and not the file?


Solution

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