2

When I first started with the Arduino environment I noticed that it had File | Preferences | Compiler Warnings set to None, so I set it to All - and there were many, many warnings in the supplied libraries! So I quickly set it to None again - until later.

Well, "later" is now, and I started going through the warnings to fix the code to quiet the compiler.

Note that I don't want to disable any warnings - I want to placate the compiler at each spot so that it says to itself "No problem here!"

  • If the compiler complains of an unused variable, I comment out the variable;
  • If it complains of a signed/unsigned comparison, I change the type;
  • If it complains of an unused parameter in a function, I...

...hmmm. If this was C++, I'd simply comment out the parameter name, leaving the type naked:

void Fn(int /*param*/) {
} // Fn(param)

Arduino reports:
error: parameter name omitted

But that isn't legal in C. With some compilers you can use the __unused attribute:

void Fn(int __unused param) {
} // Fn(param)

Arduino reports:
error: expected ';', ',' or ')' before 'param'
It's interpreting __unused as the actual parameter name

or if they're fussy:

void Fn(int __attribute__((unused)) param) {
} // Fn(param)

Arduino reports:
warning: unused parameter 'param' [-Wunused-parameter]
That's not the correct __attribute__, obviously

So none of the above work in the Arduino environment. Suggestions?


I've been programming in C and C++ for over twenty years, and I know that the #1 thing you should always do is ask the compiler to tell you about everything it notices. You need a warning-free compile at all times! Otherwise, you spend hours or days trying to find a bug - only to discover that the compiler has been warning you about it all along...

per1234
  • 4,278
  • 2
  • 24
  • 43
John Burger
  • 1,885
  • 1
  • 14
  • 23

2 Answers2

3

Well, I typed all that question up for posting, then tried one last thing. It worked!

void Fn(__attribute__((unused)) int param) {
} // Fn(param)

So, rather than let a good question go to waste: here's the answer!

Now all we need to do is get the maintainers of all the libraries to put these in... and fix the other warnings...

John Burger
  • 1,885
  • 1
  • 14
  • 23
1

Within the body of a function you can do this to suppress a warning:

void do_something(int value) {
  (void) (value);
  . . .
}

You can use a macro to do the same thing, for better clarity:

#define UNUSED(v) (void) (v)

void do_something(int value) {
  UNUSED(value);
  . . .
}

This can be helpful in situations where you have conditional compilation:

void do_something(int value) {
  #ifndef USES_PARAM
    UNUSED(value);
  #else
    other_stuff(value);
  #endif
  . . .
}
thinkyhead
  • 111
  • 2