3

I have search the solutions for C/C+ unused variable warning, and none fits my need. Here's the situation:

I am developing algorithms on PC, and will then port it to the real embedded platform. The PC version will have debug codes showing images on a monitor of a PC. The embedded platform, unfortunately, does not have a monitor. Of course the embedded platform does not need those debug code at all.

To distinguish the code, we can do this:

#ifndef EMBEDED
   MyDebugCode(parameter1, parameter2, parameter3);
#endif

However, I decide that this is too lousy, since there are so many calls of those debug functions. Those EMBEDED conditionals will pollute the code. Instead, I try to write the conditional compile flag inside the debug function like below:

inline void MyDebugCode(type1 p1, type2 p2, type3 p3)
{
#ifndef EMBEDED
     DisplaySomethingOnMonitor(p1, p2, p3);
     ...
     ...
     ...
#endif
}

So that I can write the #ifndef/#ifdef once, in the function, and call it many times without those #ifndef/#ifdef.

Now when EMBEDED is defined, we will get unused varaible p1, p2, and p3. This is the problem.

In addition, I am hoping the inline can be totally optimized out the whole debug function, as if it does not exist, when EMBEDDED is defined. This is secondary though.

Anyone has better solutions/practices?

Please note that those parameters can be references to class instances.

The compiler is g++

Robin Hsu
  • 4,164
  • 3
  • 20
  • 37
  • Doesn't your compiler have pragmas to ignore specific warnings for specific files? – OldProgrammer Dec 12 '14 at 03:25
  • I forget to search pragmas of g++ ... Wait... – Robin Hsu Dec 12 '14 at 03:28
  • Possible duplicate of [unused parameter warnings in C code](http://stackoverflow.com/q/3599160/1708801) – Shafik Yaghmour Dec 12 '14 at 03:35
  • What about `#ifdef EMBEDED` `inline void MyDebugCode(type1, type2, type3) {}` `#else` ... - sane compilers should not warn about unused anonymous parameters (because that's the whole point of anonymous parameters). – user253751 Dec 12 '14 at 03:47
  • @immibis: Wa! It's my blind spot. I can't believe I just neglect such a simple solution... Please post it as an answer, I will close this question! – Robin Hsu Dec 12 '14 at 04:07
  • @Shafik Yaghmour: This is not quite a duplicate. This special case has a simpler solution, like immibis said. Yet I am considering my secondary requirement (which is also a difference from the 'possible duplicate') now. It seems that the "optimize out" is not performed by g++, for immibis's solution. I will try more optimization options of g++ then. – Robin Hsu Dec 12 '14 at 04:13
  • @RobinHsu reposted as an answer (because it's an answer). – user253751 Dec 12 '14 at 04:47

2 Answers2

1

One possibly inelegant but common way to do this is to make MyDebugCode a macro which expands to nothing if it is undesired. A simple case would be:

#ifdef EMBEDDED
#  define MyDebugCode(a,b,c)
#else
inline void MyDebugCode(type1 p1, type2 p2, type3 p3)
{
     DisplaySomethingOnMonitor(p1, p2, p3);
     ...
     ...
     ...
}
#endif

(Some people would prefer to make MyDebugCode always a macro, either doing nothing or invoking a function with possibly a different name. YMMV.)

rici
  • 234,347
  • 28
  • 237
  • 341
  • Ya. I known this, and is considering this solution. The problem is that, the function is in a namespace. where `#define MyNameSpace::MyDebugCode(a,b,c)` won't work. Yet I am considering to move it out of namespace, or by using `use namespace MyNameSpace;` to overcome this problem. Thanks anyway. – Robin Hsu Dec 12 '14 at 05:47
1

If your compiler has no other way to disable the warning, this should work:

#ifdef EMBEDED
inline void MyDebugCode(type1, type2, type3) {}
#else
inline void MyDebugCode(type1 p1, type2 p2, type3 p3)
{
    DisplaySomethingOnMonitor(p1, p2, p3);
    ...
    ...
    ...
}
#endif

Any reasonable compiler shouldn't warn on unused unnamed parameters, because there is no way to use them.

user253751
  • 57,427
  • 7
  • 48
  • 90