當我編譯下面這段代碼優化啓用未使用變量警告
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;
}
我怎麼能編譯它沒有禁用警告,優化,並沒有垃圾代碼?
也許添加一個'(無效)比特率;'聲明。 –
如果定義了「NS3_LOG_ENABLE」,或者沿着這些線,只計算「比特率」怎麼樣?然後你的代碼實際上會被優化(不做一個不需要的計算)。 –
@ Cheersandhth.-Alf是的工作。仍然添加一些垃圾代碼,但這看起來好多了 – user000001