可视化C++条件调试

Visual C++ conditional debugging

本文关键字:调试 条件 C++ 可视化      更新时间:2023-10-16

我正在尝试设置一个带有条件调试的项目。我想要的是有一个宏debug,当我在调试模式下运行时,它 #defined 到某种 printf/cout/任何东西,而在生产模式下运行时 #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的定义来自哪里。在正确的设置中,它应该来自/不是来自您的 MSVC 项目,而不是来自其他任何地方。 在这种情况下,根据构建类型,您将拥有所期望的。

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

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

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

#define _DEBUG

这意味着即使在 DEBUG 构建中,您也不应该看到任何内容。看到输出后,这意味着 defn 存在并且它不为空。这个定义不是来自MSVC。

试试这个

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