简体   繁体   English

C++ 调试宏在发布模式下编译失败

[英]C++ Debug macro failing compile in release mode

I'm using visual studio (2019) and I have this macro:我正在使用视觉工作室(2019)并且我有这个宏:

#if _DEBUG
#define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#endif

My log function this wraps around is:我的日志功能是:

template<typename... args>
static void Log(args && ... inputs)
{
    std::osyncstream bout(std::cout);
    bout << ENGINE_TAG;
    ([&]()  {
        bout << inputs;
    } (), ...);

    bout << "\n";
}

Obviously this works fine when I'm in debug mode, but when I use release mode I get:显然,当我处于调试模式时,这可以正常工作,但是当我使用发布模式时,我得到:

Error   C3861   'EngineLog': identifier not found

I'm unsure why the compiler takes issue here, I was told it would remove the macro from the code when not defined but it seems not to be doing that.我不确定为什么编译器会在这里出现问题,我被告知它会在未定义时从代码中删除宏,但它似乎没有这样做。

if _DEBUG is not defined, then EngineLog is an unresolved name.如果_DEBUG未定义,则EngineLog是未解析的名称。 You need to add a definition for the other case.您需要为另一种情况添加定义。

#if _DEBUG
    #define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#else
    #define EngineLog(vars, ...) (0 && Log(vars, ##__VA_ARGS__))
#endif

The syntax used here prevents any potential warnings about the variadic arguments being unused while at the same time preventing both the function from being called and its parameters being evaluated.此处使用的语法可防止任何有关未使用可变参数的潜在警告,同时防止调用函数及其评估其参数。

Looks like you need a #else preprocessor case to define out the macro call with an empty expansion:看起来您需要一个#else预处理器案例来定义带有空扩展的宏调用:

#if _DEBUG
    #define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#else
    #define EngineLog(vars, ...)
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c++ 在调试中编译错误但在发布中没有 - c++ compile error in debug but not in release 如何使用 vscode 在发布模式下编译 c++ - how to compile c++ in release mode with vscode 如何设置发布/调试模式以使用 Microsoft C++ 工具集在命令提示符下编译 cpp 文件 - How to set release/debug mode to compile a cpp file at a command prompt using Microsoft C++ toolset 调试模式与发布模式下的Visual Studio(C ++)调试 - Visual Studio (C++) debugging in debug mode vs release mode C ++以发布模式进行构建,库处于调试模式 - c++ build with release mode with library in debug mode 为C ++项目同时编译发布和调试 - Compile release and debug at the same time for a C++ project C ++在Unicode Release MinDependency中编译错误,但在Debug中没有 - C++ compile errors in Unicode Release MinDependency but not in Debug 通过编码在C ++中查找调试或发布模式 - Finding Debug or Release mode in C++ through coding 如何检查可执行文件或 DLL 是在发布还是调试模式下构建的 (C++) - How to check if an executable or DLL is build in Release or Debug mode (C++) 运行时如何在 Visual Studio C++ 中确定调试或发布模式 - Runtime how to determine Debug or release mode in visual studio c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM