【问题标题】:Visual C++ conditional debuggingVisual C++ 条件调试
【发布时间】:2012-10-12 18:27:13
【问题描述】:

我正在尝试设置一个带有条件调试的项目。我想要的是有一个宏debug,当我在调试模式下运行时,它被#defined 到某种 printf/cout/anything,而在生产模式下运行时,#defined 到 null 语句。我该怎么做:

我尝试使用宏 _DEBUG,但无论我在哪种模式下运行,我总是看到我的参数打印:

struct debugger{template<typename T> debugger& operator ,(const T& v){std::cerr<<v<<" ";return *this;}}dbg;
#if _DEBUG
    #define debug(...) {dbg,__VA_ARGS__;std::cerr<<std::endl;}
#else
    #define debug(...) // Just strip off all debug tokens
#endif

在我的主要:

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int a=1,b=2,c=3;
    debug(a,b,c);
    cin>>a;
}

如果有帮助,我正在使用 Visual Studio 2012

【问题讨论】:

  • 你是如何尝试_DEBUG的?你也可以试试OutputDebugString
  • #if defined _DEBUG?或标准的#ifndef NDEBUG
  • 顺便说一句,_DEBUG 确定您要链接的 CRT,而不是编译是否调试。使用 NDEBUG 并正确定义它。
  • 您确定在构建 Release 版本时将配置设置为 Release
  • @ybungalobill 这完全不正确。 _DEBUG does 表示配置是Debug 还是Release。我认为 VC++ 根本不使用 NDEBUG。

标签: c++ visual-studio debugging c-preprocessor conditional-compilation


【解决方案1】:

您的示例中的代码是正确的。问题是_DEBUG 的定义来自哪里。在正确的设置中,它应该来自/不来自您的 MSVC 项目,而不是来自其他地方。在这种情况下,取决于构建类型,您将拥有您所期望的。

您很可能已在自己的代码中或您包含的某个标头中定义了它。

您的帖子中没有足够的信息来推断_DEBUG 的真实来源。

在调试模式下,来自 MSVC 的定义如下所示:

#define _DEBUG

这意味着即使在 DEBUG 构建中,您也不应该看到任何东西。一旦你看到输出,这意味着 defn 存在并且它不是空的。此定义并非来自 MSVC。

【讨论】:

    【解决方案2】:

    试试这个

    struct debugger{template<typename T> debugger& operator ,(const T& v){std::cerr<<v<<" ";return *this;}}dbg;
    #if _DEBUG
        #define debug(...) {dbg,__VA_ARGS__;std::cerr<<std::endl;}
    #else
        #define debug
    #endif
    

    【讨论】:

      猜你喜欢
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多