【问题标题】:#define real function name to __noop in Visual C++ 2013#define real function name to __noop in Visual C++ 2013
【发布时间】:2014-11-13 21:08:42
【问题描述】:

我可以在带有 Release (NDEBUG) 设置的 Visual C++ 2008 中做到这一点:

debug.h

#ifdef _DEBUG

void debug_printf(const char* format, ...);

#else

#define debug_printf(format, v)     __noop

#endif

debug.cpp

#include "stdafx.h"  //#include "debug.h" is inside it

void debug_printf(const char* format, ...) {
    //so much work here
}

但在 Visual C++ 2013 中不再存在,我会在 debug.cpp 文件中得到编译错误。看来我必须更改 debug.h 中的定义策略。但我想知道是否有编译器设置可以重新启用旧方式?

【问题讨论】:

  • 什么编译错误?为什么人们认为不发布它会有用?!
  • 报错信息很多:缺少';' {之前,出乎意料的类型bla2...这样,是因为debug.cpp中的debug_printf文本被替换为__noop,所以变成:void __noop(const char* format, ...) {

标签: c++ visual-studio-2013 visual-studio-2008 c-preprocessor compiler-flags


【解决方案1】:

在第一种情况下也使用宏,并让它调用实际的函数(命名与宏不同)。

在第二种情况下,只有一个空的宏体。

使用variadic macros


类似

#ifdef _DEBUG
# define debug_printf(fmt, ...) real_debug_printf(fmt, __VA_ARGS__)
#else
# define debug_printf(fmt, ...)
#endif

如果未定义 _DEBUG,则宏 debug_printf 将被任何内容(或者更确切地说,一个空行)替换。

【讨论】:

  • 想举个例子吗?
  • @suud 当然,因为什么都不做(而且非常便携!)而 __noop 似乎是特定于编译器的并且你不知道编译器是否真的生成nop 的汇编指令。
猜你喜欢
  • 2022-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 2022-12-02
  • 2018-09-22
  • 2022-12-19
  • 1970-01-01
相关资源
最近更新 更多