Suppress Compiler warning Function declared never referenced

前端 未结 9 691
你的背包
你的背包 2020-12-30 01:51

So i have some code like this:

void foo (int, int);

void bar ( )
{
    //Do Stuff

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

}

相关标签:
9条回答
  • 2020-12-30 01:58

    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.

    0 讨论(0)
  • 2020-12-30 02:00

    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.

    0 讨论(0)
  • 2020-12-30 02:01

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

    0 讨论(0)
  • 2020-12-30 02:01

    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
    0 讨论(0)
  • 2020-12-30 02:02

    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 */
    }
    
    0 讨论(0)
  • 2020-12-30 02:07

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

    0 讨论(0)
提交回复
热议问题