简体   繁体   中英

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. 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:

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

The code in your samples is correct. The problem is where the definition _DEBUG is coming from. In the right setup it should come/not come from your MSVC project and from nowhere else. 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 .

In debug mode the definition coming from MSVC will look like:

#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. This defn is coming not from 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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