当我在启用优化的情况下编译以下代码时
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);
我收到编译器警告说:
error: unused variable ‘bitrate’ [-Werror=unused-variable]
因为NS_LOG_INFO
宏被编译器优化了。为了编译,我必须添加一个无用且丑陋的代码,如下所示:
if (false) { // For suppressing "Unused variable" warning when compiling with "-d optimiized"
std::cout << bitrate << instBitrate << normBitrate << std::endl;
}
如何在不禁用警告,优化和没有垃圾代码的情况下编译它?
答案 0 :(得分:3)
由于您没有使用该变量,除非在该实例中,为什么不这样做:
NS_LOG_INFO("------RECEIVED: " << message.GetSize()
<< " Bitrate = " << (8 * m_totBytes / (Simulator::Now() - m_startTime).GetSeconds())
<< " instBit = " << instBitrate
<< " normBit = " << normBitrate);