如何在C ++程序中获得调试/释放条件编译 [英] How to obtain debug/release conditional compiling in C++ program

查看:204
本文介绍了如何在C ++程序中获得调试/释放条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个大的C ++ / Qt / QMake / qtcreator项目中,我想执行一些测试,但是只有当我使用debug标志进行编译。

In a large C++/Qt/QMake/qtcreator project I would like to perform some tests, but only when I am compiling with the debug flag.

一个方法告诉g ++的代码的一些小部分只能在调试模式下编译?

Is there a way to tell g++ that some small parts of the code have to be compiled only in debug mode ?

推荐答案

这样做的标准方法是依赖于 NDEBUG < cassert> 中定义的宏 assert()

The standard way to do this is to depend on the macro NDEBUG, which is used by the macro assert() defined in <cassert>:

#ifdef NDEBUG
  // release mode code
#else
  // debug mode code
#endif

#ifdef code> #ifndef ,当然 #else 分支是可选的。

The opposite of #ifdef is #ifndef, and of course #else branches are optional.

如果此宏无效(无论何种原因),您

If this macro doesn't work (for whatever reason), you


  1. 可以尝试 QT_NO_DEBUG ,Qt使用与 Q_ASSERT()类似的目的;和

  1. can try the macro QT_NO_DEBUG, which Qt uses for a similar purpose with Q_ASSERT(); and

应修正它,使得 NDEBUG 它需要< cassert> 才能正常工作,您使用的代码可能取决于它。

should fix it so that NDEBUG is (un)defined correctly; it's required for <cassert> to work properly, and code you use may depend on it.

这篇关于如何在C ++程序中获得调试/释放条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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