未使用的变量警告,即使在 IF 语句中显式使用它也是如此
Unused variable warning even when explicitly using it inside IF statement
我正在尝试创建(使用 C++17)一个简单的调试 header,它只在标志 LOGGER_DEBUG_MODE 时执行一些代码行已启用。这就是我的 header 的定义方式(我也尝试使用 { x; } 而不是 x 但警告仍然存在):
debug.h
#ifndef _HDEBUG
#define _HDEBUG
static bool LOGGER_DEBUG_MODE = true;
#define R_DEBUG(x) if(LOGGER_DEBUG_MODE == true) x
#endif
我包含了 debug.h 并且在我的代码的某个点我调用了宏函数 R_DEBUG 来打印一些值:
logger_adc.cpp
double anlg_device_t::eval_formula()
{
double result = -9999;
try
{
result = parser.Eval();
}
catch (mu::Parser::exception_type &e)
{
std::cout << e.GetMsg() << std::endl;
}
R_DEBUG(std::cout << "Eval Result: " << result << std::endl);
return result;
}
我希望一切正常,但是当我 运行 生成文件时,我收到了这个警告:
inc/debug.h:5:14: warning: 'LOGGER_DEBUG_MODE' defined but not used [-Wunused-variable]
static bool LOGGER_DEBUG_MODE = true;
我以为我的定义搞砸了,但在检查了 g++ 创建的临时文件后,预处理器似乎按照我的预期做了所有事情:
logger_adc.ii
double anlg_device_t::eval_formula()
{
double result = -9999;
try
{
result = parser.Eval();
}
catch (mu::Parser::exception_type &e)
{
std::cout << e.GetMsg() << std::endl;
}
if(LOGGER_DEBUG_MODE == true) std::cout << "Eval Result: " << result << std::endl;
return result;
}
为什么即使在 LOGGER_DEBUG_MODE 语句中显然使用了变量 LOGGER_DEBUG_MODE,我仍会收到警告消息?我是否搞砸了一些我没有接受的明显事情? object 文件(出现警告的地方)的编译标志是 g++ -Wall -Wextra -O1 -g -std=c++17 -save-temps=obj -Iinc -I/usr/local/include -c
加 pkg-config --cflags --libs libmodbus
如果需要,这是我的 main 函数:
main.cpp
#include "logger_adc.h"
int main()
{
anlg_device_t test (ADC_CHIP_1, 1, 18, 1, 1, true);
test.set_formula("2*x","x", test.get_voltage_ptr());
std::cout << "Test Voltage: " << test.get_voltage() << std::endl << "Test Relative: " << test.get_relative() << std::endl;
std::cout << "Test Formula (2*x): " << test.eval_formula() << std::endl;
return 0;
}
提前致谢!
您有一个 header 定义了一个 static bool LOGGER_DEBUG_MODE =true;
。如果您将 header 包含在多个 C++ 文件中,那么 每个文件都会得到它自己的那个 bool.
的副本
在你的 main.cpp 中你没有使用 R_DEBUG
所以那个 bool 的副本(大概来自 logger_adc.h
)在那个文件中确实没有被使用。
可能的解决方案是:
你应该这样做,这样你只有一个 bool 的副本(在 header 中用 extern
声明它,并在单个 C++ 文件中定义它。
使用构建定义而不是运行时检查
等等
我正在尝试创建(使用 C++17)一个简单的调试 header,它只在标志 LOGGER_DEBUG_MODE 时执行一些代码行已启用。这就是我的 header 的定义方式(我也尝试使用 { x; } 而不是 x 但警告仍然存在):
debug.h
#ifndef _HDEBUG
#define _HDEBUG
static bool LOGGER_DEBUG_MODE = true;
#define R_DEBUG(x) if(LOGGER_DEBUG_MODE == true) x
#endif
我包含了 debug.h 并且在我的代码的某个点我调用了宏函数 R_DEBUG 来打印一些值:
logger_adc.cpp
double anlg_device_t::eval_formula()
{
double result = -9999;
try
{
result = parser.Eval();
}
catch (mu::Parser::exception_type &e)
{
std::cout << e.GetMsg() << std::endl;
}
R_DEBUG(std::cout << "Eval Result: " << result << std::endl);
return result;
}
我希望一切正常,但是当我 运行 生成文件时,我收到了这个警告:
inc/debug.h:5:14: warning: 'LOGGER_DEBUG_MODE' defined but not used [-Wunused-variable] static bool LOGGER_DEBUG_MODE = true;
我以为我的定义搞砸了,但在检查了 g++ 创建的临时文件后,预处理器似乎按照我的预期做了所有事情:
logger_adc.ii
double anlg_device_t::eval_formula()
{
double result = -9999;
try
{
result = parser.Eval();
}
catch (mu::Parser::exception_type &e)
{
std::cout << e.GetMsg() << std::endl;
}
if(LOGGER_DEBUG_MODE == true) std::cout << "Eval Result: " << result << std::endl;
return result;
}
为什么即使在 LOGGER_DEBUG_MODE 语句中显然使用了变量 LOGGER_DEBUG_MODE,我仍会收到警告消息?我是否搞砸了一些我没有接受的明显事情? object 文件(出现警告的地方)的编译标志是 g++ -Wall -Wextra -O1 -g -std=c++17 -save-temps=obj -Iinc -I/usr/local/include -c
加 pkg-config --cflags --libs libmodbus
如果需要,这是我的 main 函数:
main.cpp
#include "logger_adc.h"
int main()
{
anlg_device_t test (ADC_CHIP_1, 1, 18, 1, 1, true);
test.set_formula("2*x","x", test.get_voltage_ptr());
std::cout << "Test Voltage: " << test.get_voltage() << std::endl << "Test Relative: " << test.get_relative() << std::endl;
std::cout << "Test Formula (2*x): " << test.eval_formula() << std::endl;
return 0;
}
提前致谢!
您有一个 header 定义了一个 static bool LOGGER_DEBUG_MODE =true;
。如果您将 header 包含在多个 C++ 文件中,那么 每个文件都会得到它自己的那个 bool.
在你的 main.cpp 中你没有使用 R_DEBUG
所以那个 bool 的副本(大概来自 logger_adc.h
)在那个文件中确实没有被使用。
可能的解决方案是:
你应该这样做,这样你只有一个 bool 的副本(在 header 中用 extern
声明它,并在单个 C++ 文件中定义它。
使用构建定义而不是运行时检查
等等