简体   繁体   English

Visual C ++条件调试

[英]Visual C++ conditional debugging

I am trying to setup a project with conditional debugging. 我正在尝试使用条件调试来设置项目。 What i want is to have a macro debug which is #defined to some kind of printf/cout/anything when I'm running in debug mode and #defined to null statement when running in production mode. 我想要的是进行宏debug ,在调试模式下运行时将其定义为某种printf / cout /任何内容,而在生产模式下运行时则将其定义为null语句。 How can I do this: 我怎样才能做到这一点:

I have tried using the macro _DEBUG but I always see my arguments printing regardless of which mode I am running in: 我尝试使用宏_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

In my main: 在我的主要:

using namespace std;

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

If it helps, I am using Visual studio 2012 如果有帮助,我正在使用Visual Studio 2012

The code in your samples is correct. 您的示例中的代码是正确的。 The problem is where the definition _DEBUG is coming from. 问题是定义_DEBUG来自何处。 In the right setup it should come/not come from your MSVC project and from nowhere else. 在正确的设置中,它应该来自/不来自您的MSVC项目以及其他任何地方。 In this case depending on the build type you will have what you are expecting. 在这种情况下,根据构建类型,您将获得期望的结果。

Most likely you have it defined somewhere in your own code or in one of the header that you include. 您很可能在自己的代码中或包含的标头之一中定义了它。

There is no enough info in your post to infere the true origin of _DEBUG . 您的帖子中没有足够的信息来推断_DEBUG的真实来源。

In debug mode the definition coming from MSVC will look like: 在调试模式下,来自MSVC的定义如下所示:

#define _DEBUG

This means that even in DEBUG build you should not see anything. 这意味着即使在调试版本中,您也不会看到任何内容。 Once you see the output, this means that the defn is present and it is not empty. 看到输出后,这意味着defn存在并且不为空。 This defn is coming not from MSVC. 此defn并非来自MSVC。

Try this 尝试这个

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM