0

When I compile the following piece of code with optimizations enabled

double bitrate = 8 * m_totBytes / (Simulator::Now() - m_startTime).GetSeconds();
double instBitrate = 8 * message.GetSize() / (Simulator::Now() - m_lastRecv).GetSeconds();
normBitrate = 0.1 * instBitrate + 0.9 * m_resolution;

NS_LOG_INFO("------RECEIVED: " << message.GetSize()
         << " Bitrate = " << bitrate
         << " instBit = " << instBitrate
         << " normBit = " << normBitrate);

I get a compiler warning saying:

error: unused variable ‘bitrate’ [-Werror=unused-variable]

because the NS_LOG_INFO macro gets optimized out by the compiler. In order to compile I have to add a useless and ugly bit of code like the following:

if (false) { // For suppressing "Unused variable" warning when compiling with "-d optimiized"
    std::cout << bitrate << instBitrate << normBitrate << std::endl;
}

How can I compile it without disabling the warnings, the optimization, and no junk code?

Kara
  • 6,115
  • 16
  • 50
  • 57
user000001
  • 32,226
  • 12
  • 81
  • 108

1 Answers1

3

Since you're not using that variable except for in that instance, why not just do:

NS_LOG_INFO("------RECEIVED: " << message.GetSize()
     << " Bitrate = " << (8 * m_totBytes / (Simulator::Now() - m_startTime).GetSeconds())
     << " instBit = " << instBitrate
     << " normBit = " << normBitrate);
splrs
  • 2,424
  • 2
  • 19
  • 29
  • Yes this works. Still interested in Taylor's suggestion, so that the calculations are only executed when necessary. – user000001 Dec 10 '13 at 16:59
  • You stipulated no junk cod so I stuck to that brief, checking for the define of course should work too :-) – splrs Dec 10 '13 at 17:01
  • 1
    @user000001 well the calculation is in a macro which gets expanded to nothing, so this is essentially doing what I was saying, just without the extra variable declaration, which is probably cleaner. If you wanted the variable declaration for some reason, I believe you could use `#ifdef NS3_LOG_ENABLE`. – Taylor Brandstetter Dec 10 '13 at 17:09