Suppress Compiler warning Function declared never

2020-06-01 07:07发布

问题:

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?

回答1:

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



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

A good way to encapsulate compiler- and system-dependent stuff is to factor it out into headers. Then you adjust the include path depending on the compiler and system and perhaps other things. You can do the same for source code files.

In this case the declaration doesn't seem to depend on compiler- or system, so just add the following common header:

// [foo.h]
#pragma once
void foo( int, int );

With implementation file

// [foo.cpp]
#include <foo.virtual.cpp>

Then for the build where something should happen, add to the include path a directory containing

// [foo.virtual.cpp]
#include <foo.h>
void foo( int const a, int const b )
{
    // Do the thing.
}

And for the build where nothing should happen, add to the include path a directory containing

// [foo.virtual.cpp]
#include <foo.h>
void foo( int, int ) {}

If you are afraid that the call of an empty function will be very time consuming, like, a nano-second wasted, then simply move the definitions to headers and add the word inline.

If foo is also used for other purposes, define a function bar that calls it for the should-or-should-not-happen thing, and do the above for bar instead of for foo.

Then, you have removed all the preprocessor stuff.

Remember that preprocessor directives in code are ungood.



回答6:

I find a way to do that globally and it works also in c

#define SUPPRESS_UNUSED_WARN(var) \
    int _dummy_tmp_##var = ((int)(var) & 0)

then you use it like:

static int foo(int a, int b)
{
    // ....
}
SUPRESS_UNUSED_WARN(foo);
  • it can be used on functions and global variables
  • it should be placed globally in order to work work
  • it can't be used for local variables


回答7:

For ARM target platform while using, ARM compiler, Use the following compiler directive around the target function to suppress "Warning[Pe177]: function declared but never referenced" warning messages:

#pragma diag_suppress=Pe177
void foo(void)
{
/* does something but is not being called for the current build */
}


回答8:

#define SUPPRESS_UNUSED_WARN(var) \
    int _dummy_tmp_##var = ((int)(var) & 0)

not work in IAR, change to this will work:

#define SUPPRESS_UNUSED_WARN(var) \
     void _dummy_tmp_##var(void) { (void)(var); }


回答9:

You can also define _CRT_SECURE_NO_DEPRECATE macro in Visual studio project settings.

Goto project Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

Add _CRT_SECURE_NO_DEPRECATE.

Thats it.!