Unused variable warning when optimization is set

503 views Asked by At

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?

1

There are 1 answers

3
splrs On BEST ANSWER

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);