2

我想用條件調試來設置一個項目。我想要的是有一個宏debug這是#defined某種printf/cout /任何東西,當我在調試模式下運行和#defined爲生產模式下運行時爲空語句。我怎樣才能做到這一點:Visual C++有條件的調試

我已經使用宏_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

+0

你是怎樣嘗試'_DEBUG'?你也可以嘗試'OutputDebugString'。 – chris

+0

@chris:已更新 – prongs

+0

'#if defined _DEBUG'?或標準的'#ifndef NDEBUG' –

回答

0

您樣本中的代碼是正確的。問題在於定義_DEBUG來自哪裏。在正確的設置中,它應該來自/不是來自你的MSVC項目,也不是來自其他地方。在這種情況下,根據構建類型,您將擁有您所期望的。

很可能你已經在自己的代碼或其中一個頭文件中定義了它。

您的帖子中沒有足夠的信息來推斷_DEBUG的真實來源。

在調試模式從MSVC來定義將看起來像:

#define _DEBUG 

這意味着,即使在DEBUG構建你不應該看不到任何東西。一旦你看到輸出,這意味着defn存在並且不是空的。這個定義來自MSVC。

0

試試這個

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