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,2782 gold badges23 silver badges43 bronze badges
asked Jun 17, 2016 at 7:23
1
  • The Arduino core is a mixture of C and C++. The void f(int) {...} idiom should work in the C++ files. I agree it's a shame the core generates so many warnings. Commented Jun 17, 2016 at 8:12

2 Answers 2

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

answered Jun 17, 2016 at 7:23
1
  • Another idiom often used in C is void f(int param) { (void) param; ... }. Commented Jun 17, 2016 at 8:12
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
 . . .
}
answered Jul 17, 2016 at 0:44
1
  • I know most compilers will do the "right thing" with that, but I've seen some that emit the "load" - and then do nothing with it. The nice thing about "decorating" a variable declaration is that it does two things: it signals the compiler, not the code generation part; and it merely flags a possibility, not a guarantee. If you actually use a variable that is flagged as unused, then the compler doesn't complain. So in your final example, you decorate value and don't need both sides of the #if[n]def Commented Jul 17, 2016 at 9:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.