Suppress Compiler warning Function declared never referenced

前端 未结 9 692
你的背包
你的背包 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 02:16

    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.

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

    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.

    0 讨论(0)
  • 2020-12-30 02:20
    #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); }
    
    0 讨论(0)
提交回复
热议问题