使用#if定义的(_DEBUG) [英] Use of #if defined(_DEBUG)

查看:440
本文介绍了使用#if定义的(_DEBUG)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于C ++ MFC的应用程序.有时,我会添加#if defined(_DEBUG)语句,以便在开发过程中为我提供帮助.我的经理要求我删除所有此类语句,因为他不希望使用两个版本的代码.我的感觉是,如果没有使用#if defined(_DEBUG)的能力,则在开发过程中漏洞更容易被发现.

I am writing a C++ MFC-based application. Occasionally I will add #if defined(_DEBUG) statements in order to help me during the development process. My manager has demanded that I remove all such statements as he doesn't want two versions of the code. My feeling is that without the ability to use #if defined(_DEBUG), bugs are more likely to creep in undetected during the development process.

其他人对此有任何想法吗?

Does anyone else have any thoughts on this?

推荐答案

将调试代码从生产代码中编译出来是有利有弊的.

There are pros and cons for having debug code compiled out of production code.

一些优点:

  • 生产可执行文件将变小
  • 不会浪费资源来准备从未使用过的日志消息,统计信息等
  • 如果仅由调试代码使用,则可以删除对外部库的依赖

一些缺点:

  • 您最终得到了两个不同的可执行文件
  • 由于某些调试日志在生产版本中不可用,这使得在现场进行故障排除更加困难
  • 两个版本的行为有可能不相同.例如,由于测试是在调试版本中完成的,因此错误可能会在生产版本中出现,而在测试期间则不会出现.
  • 版本之间存在不兼容的风险,例如文件格式略有不同(如eranb的回答所示).

尤其是,如果使用编译出的代码(甚至是assert()调用),请务必注意调试代码没有副作用.否则,您将创建会在打开调试功能时消失的错误.

In particular, if using code compiled out (even assert() calls) be very careful that the debug code has no side effects. Otherwise you will create bugs which disappear when debugging is turned on.

通过使用体面的日志记录框架,您至少可以实现某些优点.例如,我使用 rLog 取得了一些成功-我喜欢它的一件事是优化以最大程度地减少休眠日志记录语句的开销.其他更多最新的日志记录框架也提供了类似的功能.

You can achieve at least some of pros by using a decent logging framework. For example, I have had some success using rLog - one of the things I like about it is that it is optimized to minimize overhead of dormant logging statements. Other more up to date logging frameworks provide similar functionality.

说过,我使用过的每个C ++环境都至少在某种程度上进行调试代码的编译/编译.例如,根据NDEBUG宏, assert()会被编译进/输出. ASSERT()/_DEBUG似乎是该主题上特定于MSVC的变体(尽管据我所知,MSVC还支持更标准的assert()/NDEBUG).

Having said that, every C++ environment I have worked with has at least some level of compile in/out of debug code. For example, assert() is compiled in/out depending on the NDEBUG macro. The ASSERT() / _DEBUG seems to be an MSVC specific variation on that theme (although as far as I know MSVC also supports the more standard assert() / NDEBUG).

这篇关于使用#if定义的(_DEBUG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆