我正在尝试创建(使用 C++17)一个简单的调试头,如果启用了标志LOGGER_DEBUG_MODE ,它只执行一些代码行。这就是我的标题的定义方式(我也尝试使用{ x; }而不是x但警告仍然存在):
调试.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;
}
我希望一切都能正常工作,但是当我运行 makefile 时,我收到了这个警告:
inc/debug.h:5:14: 警告: 'LOGGER_DEBUG_MODE' 已定义但未使用 [-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在if语句中明确使用,我也会收到警告消息?我是否搞砸了一些我没有注意到的明显事情?我的目标文件(出现警告的地方)的编译标志是g++ -Wall -Wextra -O1 -g -std=c++17 -save-temps=obj -Iinc -I/usr/local/include -c加号pkg-config --cflags --libs libmodbus
如果需要,这是我的主要功能:
主文件
#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;
}
提前致谢!