2

I have a single function for debugging messages, used through all the project. When KEYDEBUG is defined as 1 or 0, the function is enabled or disabled as required.

#define KEYDEBUG 0

void debugging(int var){
   #if KEYDEBUG
   //Assert
   #endif
}

When checking the compiler, the variable var raises a warning -Wunused-parameter. How should remove this warning just at this part of the project? Or which other alternative for using debugging code and removing it from memory for production release?

2

In your case you do not use the entire body, so you can use a macro.

This way the code when KEYDEBUG is 0, will be like the complete function would not exist (except for the void cast, see below).

#include "assert.h"

#define KEYDEBUG 0

#ifndef KEYDEBUG
#define debugging(x) void(x);
#endif


void debugging(int var){
   assert(var);
}

void setup() {
  // put your setup code here, to run once:
  debugging(1);
}

The information below is for not using the function parameter, but keep the function (in case you have code in your function that also does something when KEYDEBUG is 0.

Unused attribute

The cleanest way I could find is using the unused attribute:

#if KEYDEBUG
   void debugging(__attribute__((unused)) int var){
      //Assert
#else
   void debugging(int var){
      //Assert
#endif
   } 

or if you prefer better readability:

#if KEYDEBUG
   void debugging(__attribute__((unused)) int var){
      //Assert
   }
#else
   void debugging(int var){
      //Assert
   }
#endif

Cast to void

Less clean but does the job:

void debugging(int var){
   #if KEYDEBUG
      //Assert
   #else
      (void) var; // prevent unused parameter warning
   #endif
}

Macros/Parameter

It's not easy to remove the variable since it's used in a function and will be called from many places probably. You could maybe you use a macro but that gets messy soon.

This solution works, and afaik the compiler will optimize the code out (if not , it will cost very less anyway).

  • It is possible to remove absolutely the debugging function and its calls without touching the debugging calls? – Brethlosze Jul 16 at 21:22
  • 1
    Not fully, well in the macro I defined, the function call is replaced by a void cast, so this is not a function, and you don't have to touch the debugging calls. – Michel Keijzers Jul 16 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.