简体   繁体   English

在Visual Studio 2008中使用_DEBUG预处理程序定义-C ++

[英]Using _DEBUG Preprocessor Defintion in Visual Studio 2008 - C++

I have a VC++ application written using Visual Studio 2008. For debug purposes, timeout values compile differently depending on the type of build (Debug or Release). 我有一个使用Visual Studio 2008编写的VC ++应用程序。出于调试目的,超时值的编译取决于生成类型(调试或发行版)。 The code sample below is typical of how I am trying to do this. 下面的代码示例是我尝试执行此操作的典型示例。

#ifdef _DEBUG
if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, INFINITE )) != WAIT_OBJECT_0 )
#else
if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, BAS_THREE_SEC_TIMEOUT )) != WAIT_OBJECT_0 )
#endif
{
    /* ... */
}

The Debug configuration has _DEBUG defined in the Preprocessor Definitions. 调试配置在预处理器定义中定义了_DEBUG The Release configuration does not have it. 发布配置没有它。 For each respective configuration, the expected lined is grayed out (presumably to indicate that the other line will be compiled in). 对于每个相应的配置,期望的行都显示为灰色(大概表示将插入另一行)。

However, at run-time, the Release build timeouts remain INFINITE . 但是,在运行时,Release构建超时仍为INFINITE When I try to set a breakpoint on both if statements and try to run the Release code, the breakpoint on the first if statement remains, whereas the other breakpoint gets moved down to the first line inside the brackets. 当我尝试在两个if语句上都设置一个断点并尝试运行Release代码时,第一个if语句上的断点仍然保留,而另一个断点则向下移动到括号内的第一行。

What gives? 是什么赋予了? How do I make this compile option work? 如何使此编译选项起作用? Should I be using something else? 我应该使用其他东西吗?

Something like this instead perhaps? 也许是这样的东西?

#ifdef _DEBUG
#define NONMS_WAIT_TIMEOUT INFINITE
#else
#define NONMS_WAIT_TIMEOUT BAS_THREE_SEC_TIMEOUT
#endif

if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, NONMS_WAIT_TIMEOUT)) != WAIT_OBJECT_0 )
{
    /* ... */
}

Edit: Debug builds in VS should have _DEBUG defined and release builds should have NDEBUG defined. 编辑:VS中的调试版本应具有_DEBUG定义,发布版本应具有NDEBUG定义。 Check your project pre-processor directives to make sure this is what you have. 检查您的项目预处理器指令,以确保这是您所拥有的。

Write it like this and build in the Release build: 这样写,然后在Release版本中构建:

#ifdef _DEBUG
#error Something is really wrong, _DEBUG is still defined
if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, INFINITE )) != WAIT_OBJECT_0 )
#else
if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, BAS_THREE_SEC_TIMEOUT )) != WAIT_OBJECT_0 )
#endif

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

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